blob: e88b2bb9eee51d761d9909c5ac089efa4689cefa [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"
Yuly Novikovb56ddbb2018-11-02 16:53:18 -040019#include "libANGLE/Display.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050020#include "libANGLE/renderer/driver_utils.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050021#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050022#include "libANGLE/renderer/vulkan/CompilerVk.h"
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040023#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050024#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050025#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050026#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050027#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050028#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050029#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050030#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040031
Shahbaz Youssefi61656022018-10-24 15:00:50 -040032#include "third_party/trace_event/trace_event.h"
33
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070034// Consts
35namespace
36{
Jamie Madill7c985f52018-11-29 18:16:17 -050037const uint32_t kMockVendorID = 0xba5eba11;
38const uint32_t kMockDeviceID = 0xf005ba11;
39constexpr char kMockDeviceName[] = "Vulkan Mock Device";
40constexpr size_t kInFlightCommandsLimit = 100u;
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -050041constexpr VkFormatFeatureFlags kInvalidFormatFeatureFlags = static_cast<VkFormatFeatureFlags>(-1);
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070042} // anonymous namespace
43
Jamie Madill9e54b5a2016-05-25 12:57:39 -040044namespace rx
45{
46
Jamie Madille09bd5d2016-11-29 16:20:35 -050047namespace
48{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040049// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
50// one for the vertex shader.
51constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040052// Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes)
53static constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -040054// Wait a maximum of 10s. If that times out, we declare it a failure.
55static constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
Jamie Madille09bd5d2016-11-29 16:20:35 -050056
Omar El Sheikh26c61b22018-06-29 12:50:59 -060057bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
58{
59#if !defined(ANGLE_PLATFORM_ANDROID)
60 // Mock ICD does not currently run on Android
61 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
62 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
63 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
64#else
65 return false;
66#endif // !defined(ANGLE_PLATFORM_ANDROID)
67}
68
Jamie Madille09bd5d2016-11-29 16:20:35 -050069VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
70 const std::vector<const char *> &enabledExtensionNames)
71{
72 // Compile the extensions names into a set.
73 std::set<std::string> extensionNames;
74 for (const auto &extensionProp : extensionProps)
75 {
76 extensionNames.insert(extensionProp.extensionName);
77 }
78
Jamie Madillacf2f3a2017-11-21 19:22:44 -050079 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050080 {
81 if (extensionNames.count(extensionName) == 0)
82 {
83 return VK_ERROR_EXTENSION_NOT_PRESENT;
84 }
85 }
86
87 return VK_SUCCESS;
88}
89
Tobin Ehlis3a181e32018-08-29 15:17:05 -060090// Array of Validation error/warning messages that will be ignored, should include bugID
91constexpr std::array<const char *, 1> kSkippedMessages = {
92 // http://anglebug.com/2796
93 " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) "
94 "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader "
95 "corresponding to VK_SHADER_STAGE_VERTEX_BIT."};
96
97// Suppress validation errors that are known
98// return "true" if given code/prefix/message is known, else return "false"
99bool IsIgnoredDebugMessage(const char *message)
100{
101 for (const auto &msg : kSkippedMessages)
102 {
103 if (strcmp(msg, message) == 0)
104 {
105 return true;
106 }
107 }
108 return false;
109}
110
Yuly Novikov199f4292018-01-19 19:04:05 -0500111VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
112 VkDebugReportObjectTypeEXT objectType,
113 uint64_t object,
114 size_t location,
115 int32_t messageCode,
116 const char *layerPrefix,
117 const char *message,
118 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500119{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600120 if (IsIgnoredDebugMessage(message))
121 {
122 return VK_FALSE;
123 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500124 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
125 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500126 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500127#if !defined(NDEBUG)
128 // Abort the call in Debug builds.
129 return VK_TRUE;
130#endif
131 }
132 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
133 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500134 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500135 }
136 else
137 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500138 // Uncomment this if you want Vulkan spam.
139 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500140 }
141
142 return VK_FALSE;
143}
144
Yuly Novikov199f4292018-01-19 19:04:05 -0500145// If we're loading the validation layers, we could be running from any random directory.
146// Change to the executable directory so we can find the layers, then change back to the
147// previous directory to be safe we don't disrupt the application.
148class ScopedVkLoaderEnvironment : angle::NonCopyable
149{
150 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600151 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
152 : mEnableValidationLayers(enableValidationLayers),
153 mEnableMockICD(enableMockICD),
154 mChangedCWD(false),
155 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500156 {
157// Changing CWD and setting environment variables makes no sense on Android,
158// since this code is a part of Java application there.
159// Android Vulkan loader doesn't need this either.
160#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600161 if (enableMockICD)
162 {
163 // Override environment variable to use built Mock ICD
164 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
165 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
166 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
167 if (!mChangedICDPath)
168 {
169 ERR() << "Error setting Path for Mock/Null Driver.";
170 mEnableMockICD = false;
171 }
172 }
Jamie Madill46848422018-08-09 10:46:06 -0400173 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500174 {
175 const auto &cwd = angle::GetCWD();
176 if (!cwd.valid())
177 {
178 ERR() << "Error getting CWD for Vulkan layers init.";
179 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400180 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500181 }
182 else
183 {
184 mPreviousCWD = cwd.value();
185 const char *exeDir = angle::GetExecutableDirectory();
186 mChangedCWD = angle::SetCWD(exeDir);
187 if (!mChangedCWD)
188 {
189 ERR() << "Error setting CWD for Vulkan layers init.";
190 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400191 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500192 }
193 }
194 }
195
196 // Override environment variable to use the ANGLE layers.
197 if (mEnableValidationLayers)
198 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700199 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500200 {
201 ERR() << "Error setting environment for Vulkan layers init.";
202 mEnableValidationLayers = false;
203 }
204 }
205#endif // !defined(ANGLE_PLATFORM_ANDROID)
206 }
207
208 ~ScopedVkLoaderEnvironment()
209 {
210 if (mChangedCWD)
211 {
212#if !defined(ANGLE_PLATFORM_ANDROID)
213 ASSERT(mPreviousCWD.valid());
214 angle::SetCWD(mPreviousCWD.value().c_str());
215#endif // !defined(ANGLE_PLATFORM_ANDROID)
216 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600217 if (mChangedICDPath)
218 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600219 if (mPreviousICDPath.value().empty())
220 {
221 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
222 }
223 else
224 {
225 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
226 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600227 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500228 }
229
Jamie Madillaaca96e2018-06-12 10:19:48 -0400230 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500231
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600232 bool canEnableMockICD() const { return mEnableMockICD; }
233
Yuly Novikov199f4292018-01-19 19:04:05 -0500234 private:
235 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600236 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500237 bool mChangedCWD;
238 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600239 bool mChangedICDPath;
240 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500241};
242
Jamie Madill21061022018-07-12 23:56:30 -0400243void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
244 bool preferMockICD,
245 VkPhysicalDevice *physicalDeviceOut,
246 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
247{
248 ASSERT(!physicalDevices.empty());
249 if (preferMockICD)
250 {
251 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
252 {
253 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
254 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
255 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
256 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
257 {
258 *physicalDeviceOut = physicalDevice;
259 return;
260 }
261 }
262 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
263 "physicalDevice instead.";
264 }
265
266 // Fall back to first device.
267 *physicalDeviceOut = physicalDevices[0];
268 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
269}
Jamie Madill0da73fe2018-10-02 09:31:39 -0400270
271// Initially dumping the command graphs is disabled.
272constexpr bool kEnableCommandGraphDiagnostics = false;
Ian Elliottbcb78902018-12-19 11:46:29 -0700273
274bool ExtensionFound(const char *extensionName,
275 const std::vector<VkExtensionProperties> &extensionProps)
276{
277 for (const auto &extensionProp : extensionProps)
278 {
279 if (strcmp(extensionProp.extensionName, extensionName) == 0)
280 {
281 return true;
282 }
283 }
284 return false;
285}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500286} // anonymous namespace
287
Jamie Madill49ac74b2017-12-21 14:42:33 -0500288// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400289RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500290
Jamie Madillaaca96e2018-06-12 10:19:48 -0400291RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500292
293RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
294 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
Jamie Madillb980c562018-11-27 11:34:27 -0500295{}
Jamie Madill49ac74b2017-12-21 14:42:33 -0500296
297RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
298{
299 std::swap(commandPool, other.commandPool);
300 std::swap(fence, other.fence);
301 std::swap(serial, other.serial);
302 return *this;
303}
304
Jamie Madillbea35a62018-07-05 11:54:10 -0400305void RendererVk::CommandBatch::destroy(VkDevice device)
306{
307 commandPool.destroy(device);
308 fence.destroy(device);
309}
310
Jamie Madill9f2a8612017-11-30 12:43:09 -0500311// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500312RendererVk::RendererVk()
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400313 : mDisplay(nullptr),
314 mCapsInitialized(false),
Ian Elliottbcb78902018-12-19 11:46:29 -0700315 mFeaturesInitialized(false),
Jamie Madill0448ec82016-12-23 13:41:47 -0500316 mInstance(VK_NULL_HANDLE),
317 mEnableValidationLayers(false),
Jamie Madill0ea96212018-10-30 15:14:51 -0400318 mEnableMockICD(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500319 mDebugReportCallback(VK_NULL_HANDLE),
320 mPhysicalDevice(VK_NULL_HANDLE),
321 mQueue(VK_NULL_HANDLE),
322 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
323 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400324 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400325 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400326 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400327 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400328 mCommandGraph(kEnableCommandGraphDiagnostics),
329 mGpuEventsEnabled(false),
330 mGpuClockSync{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()},
331 mGpuEventTimestampOrigin(0)
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500332{
333 VkFormatProperties invalid = {0, 0, kInvalidFormatFeatureFlags};
334 mFormatProperties.fill(invalid);
335}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400336
Jamie Madillb980c562018-11-27 11:34:27 -0500337RendererVk::~RendererVk() {}
Jamie Madill21061022018-07-12 23:56:30 -0400338
339void RendererVk::onDestroy(vk::Context *context)
340{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500341 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500342 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500343 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400344 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500345 }
346
Shahbaz Youssefie3219402018-12-08 16:54:14 +0100347 mUtils.destroy(mDevice);
Shahbaz Youssefi8f1b7a62018-11-14 16:02:54 -0500348
Jamie Madillc7918ce2018-06-13 13:25:31 -0400349 mPipelineLayoutCache.destroy(mDevice);
350 mDescriptorSetLayoutCache.destroy(mDevice);
351
Jamie Madill9f2a8612017-11-30 12:43:09 -0500352 mRenderPassCache.destroy(mDevice);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500353 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400354 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400355 mShaderLibrary.destroy(mDevice);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400356 mGpuEventQueryPool.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500357
Jamie Madill06ca6342018-07-12 15:56:53 -0400358 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500359
Jamie Madill5deea722017-02-16 10:44:46 -0500360 if (mCommandPool.valid())
361 {
362 mCommandPool.destroy(mDevice);
363 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500364
365 if (mDevice)
366 {
367 vkDestroyDevice(mDevice, nullptr);
368 mDevice = VK_NULL_HANDLE;
369 }
370
Jamie Madill0448ec82016-12-23 13:41:47 -0500371 if (mDebugReportCallback)
372 {
373 ASSERT(mInstance);
374 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
375 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
376 ASSERT(destroyDebugReportCallback);
377 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
378 }
379
Jamie Madill4d0bf552016-12-28 15:45:24 -0500380 if (mInstance)
381 {
382 vkDestroyInstance(mInstance, nullptr);
383 mInstance = VK_NULL_HANDLE;
384 }
385
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600386 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500387 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500388}
389
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400390void RendererVk::notifyDeviceLost()
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400391{
392 mDeviceLost = true;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400393
394 mCommandGraph.clear();
395 mLastSubmittedQueueSerial = mCurrentQueueSerial;
396 mCurrentQueueSerial = mQueueSerialFactory.generate();
397 freeAllInFlightResources();
398
399 mDisplay->notifyDeviceLost();
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400400}
401
402bool RendererVk::isDeviceLost() const
403{
404 return mDeviceLost;
405}
406
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400407angle::Result RendererVk::initialize(DisplayVk *displayVk,
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400408 egl::Display *display,
Jamie Madill21061022018-07-12 23:56:30 -0400409 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500410{
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400411 mDisplay = display;
412 const egl::AttributeMap &attribs = mDisplay->getAttributeMap();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600413 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
414 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500415 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madill0ea96212018-10-30 15:14:51 -0400416 mEnableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500417
Jamie Madill0448ec82016-12-23 13:41:47 -0500418 // Gather global layer properties.
419 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400420 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500421
422 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
423 if (instanceLayerCount > 0)
424 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400425 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
426 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500427 }
428
Jamie Madille09bd5d2016-11-29 16:20:35 -0500429 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400430 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400431 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500432
433 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
434 if (instanceExtensionCount > 0)
435 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400436 ANGLE_VK_TRY(displayVk,
437 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
438 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500439 }
440
Yuly Novikov199f4292018-01-19 19:04:05 -0500441 const char *const *enabledLayerNames = nullptr;
442 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500443 if (mEnableValidationLayers)
444 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500445 bool layersRequested =
446 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
447 mEnableValidationLayers = GetAvailableValidationLayers(
448 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500449 }
450
Jamie Madille09bd5d2016-11-29 16:20:35 -0500451 std::vector<const char *> enabledInstanceExtensions;
452 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500453 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500454
Jamie Madill0448ec82016-12-23 13:41:47 -0500455 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
456 if (mEnableValidationLayers)
457 {
458 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
459 }
460
Jamie Madille09bd5d2016-11-29 16:20:35 -0500461 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400462 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400463 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500464
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400465 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500466 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500467 applicationInfo.pApplicationName = "ANGLE";
468 applicationInfo.applicationVersion = 1;
469 applicationInfo.pEngineName = "ANGLE";
470 applicationInfo.engineVersion = 1;
Ian Elliott899c5d22018-12-21 13:12:50 -0700471
472 auto enumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
473 vkGetInstanceProcAddr(mInstance, "vkEnumerateInstanceVersion"));
474 if (!enumerateInstanceVersion)
475 {
476 applicationInfo.apiVersion = VK_API_VERSION_1_0;
477 }
478 else
479 {
480 uint32_t apiVersion = VK_API_VERSION_1_0;
481 ANGLE_VK_TRY(displayVk, enumerateInstanceVersion(&apiVersion));
482 if ((VK_VERSION_MAJOR(apiVersion) > 1) || (VK_VERSION_MINOR(apiVersion) >= 1))
483 {
484 // Note: will need to revisit this with Vulkan 1.2+.
485 applicationInfo.apiVersion = VK_API_VERSION_1_1;
486 }
487 else
488 {
489 applicationInfo.apiVersion = VK_API_VERSION_1_0;
490 }
491 }
Jamie Madill327ba852016-11-30 12:38:28 -0500492
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400493 VkInstanceCreateInfo instanceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -0500494 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
495 instanceInfo.flags = 0;
496 instanceInfo.pApplicationInfo = &applicationInfo;
Jamie Madill327ba852016-11-30 12:38:28 -0500497
Jamie Madille09bd5d2016-11-29 16:20:35 -0500498 // Enable requested layers and extensions.
499 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
500 instanceInfo.ppEnabledExtensionNames =
501 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500502 instanceInfo.enabledLayerCount = enabledLayerCount;
503 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500504
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400505 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500506
Jamie Madill0448ec82016-12-23 13:41:47 -0500507 if (mEnableValidationLayers)
508 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400509 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500510
511 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500512 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
513 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
514 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
515 debugReportInfo.pfnCallback = &DebugReportCallback;
516 debugReportInfo.pUserData = this;
517
518 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
519 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
520 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400521 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
522 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500523 }
524
Jamie Madill4d0bf552016-12-28 15:45:24 -0500525 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400526 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
527 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500528
529 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700530 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400531 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
532 physicalDevices.data()));
Jamie Madill0ea96212018-10-30 15:14:51 -0400533 ChoosePhysicalDevice(physicalDevices, mEnableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700534 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500535
Jamie Madill30b5d842018-08-31 17:19:12 -0400536 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
537
Jamie Madill4d0bf552016-12-28 15:45:24 -0500538 // Ensure we can find a graphics queue family.
539 uint32_t queueCount = 0;
540 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
541
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400542 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500543
544 mQueueFamilyProperties.resize(queueCount);
545 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
546 mQueueFamilyProperties.data());
547
Jamie Madillb980c562018-11-27 11:34:27 -0500548 size_t graphicsQueueFamilyCount = false;
549 uint32_t firstGraphicsQueueFamily = 0;
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500550 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500551 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
552 {
553 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500554 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500555 {
556 ASSERT(queueInfo.queueCount > 0);
557 graphicsQueueFamilyCount++;
558 if (firstGraphicsQueueFamily == 0)
559 {
560 firstGraphicsQueueFamily = familyIndex;
561 }
562 break;
563 }
564 }
565
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400566 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500567
568 // If only one queue family, go ahead and initialize the device. If there is more than one
569 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
570 if (graphicsQueueFamilyCount == 1)
571 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400572 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500573 }
574
Jamie Madill035fd6b2017-10-03 15:43:22 -0400575 // Store the physical device memory properties so we can find the right memory pools.
576 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500577
Jamie Madill06ca6342018-07-12 15:56:53 -0400578 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500579
Jamie Madill6a89d222017-11-02 11:59:51 -0400580 // Initialize the format table.
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500581 mFormatTable.initialize(this, &mNativeTextureCaps, &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400582
Jamie Madill7c985f52018-11-29 18:16:17 -0500583 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400584}
585
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400586angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500587{
588 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400589 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400590 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500591
592 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
593 if (deviceLayerCount > 0)
594 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400595 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
596 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500597 }
598
599 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400600 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
601 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500602
603 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
604 if (deviceExtensionCount > 0)
605 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400606 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
607 &deviceExtensionCount,
608 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609 }
610
Yuly Novikov199f4292018-01-19 19:04:05 -0500611 const char *const *enabledLayerNames = nullptr;
612 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500613 if (mEnableValidationLayers)
614 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500615 mEnableValidationLayers = GetAvailableValidationLayers(
616 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500617 }
618
619 std::vector<const char *> enabledDeviceExtensions;
620 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
621
Ian Elliottbcb78902018-12-19 11:46:29 -0700622 initFeatures(deviceExtensionProps);
623 mFeaturesInitialized = true;
624
Luc Ferronbf6dc372018-06-28 15:24:19 -0400625 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
Ian Elliott52f5da42018-12-21 09:02:09 -0700626 if ((getFeatures().flipViewportY) &&
627 (mPhysicalDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0)))
Luc Ferronbf6dc372018-06-28 15:24:19 -0400628 {
629 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
630 }
Ian Elliottbcb78902018-12-19 11:46:29 -0700631 if (getFeatures().supportsIncrementalPresent)
632 {
633 enabledDeviceExtensions.push_back(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME);
634 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400635
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400636 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500637
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400638 // Select additional features to be enabled
639 VkPhysicalDeviceFeatures enabledFeatures = {};
640 enabledFeatures.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
641
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400642 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500643
644 float zeroPriority = 0.0f;
645
646 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500647 queueCreateInfo.flags = 0;
648 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
649 queueCreateInfo.queueCount = 1;
650 queueCreateInfo.pQueuePriorities = &zeroPriority;
651
652 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400653 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500654
Jamie Madill50cf2be2018-06-15 09:46:57 -0400655 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400656 createInfo.flags = 0;
657 createInfo.queueCreateInfoCount = 1;
658 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500659 createInfo.enabledLayerCount = enabledLayerCount;
660 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500661 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
662 createInfo.ppEnabledExtensionNames =
663 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400664 createInfo.pEnabledFeatures = &enabledFeatures;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500665
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400666 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500667
668 mCurrentQueueFamilyIndex = queueFamilyIndex;
669
670 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
671
672 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400673 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -0500674 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
675 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
676 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500677
Yuly Novikov27780292018-11-09 11:19:49 -0500678 ANGLE_VK_TRY(displayVk, mCommandPool.init(mDevice, commandPoolInfo));
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400679
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400680 // Initialize the vulkan pipeline cache.
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500681 ANGLE_TRY(initPipelineCache(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500682
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400683 // Initialize the submission semaphore pool.
684 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
685
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400686#if ANGLE_ENABLE_VULKAN_GPU_TRACE_EVENTS
687 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
688 ASSERT(platform);
689
690 // GPU tracing workaround for anglebug.com/2927. The renderer should not emit gpu events during
691 // platform discovery.
692 const unsigned char *gpuEventsEnabled =
693 platform->getTraceCategoryEnabledFlag(platform, "gpu.angle.gpu");
694 mGpuEventsEnabled = gpuEventsEnabled && *gpuEventsEnabled;
695#endif
696
697 if (mGpuEventsEnabled)
698 {
699 // Calculate the difference between CPU and GPU clocks for GPU event reporting.
700 ANGLE_TRY(mGpuEventQueryPool.init(displayVk, VK_QUERY_TYPE_TIMESTAMP,
701 vk::kDefaultTimestampQueryPoolSize));
702 ANGLE_TRY(synchronizeCpuGpuTime(displayVk));
703 }
704
Jamie Madill7c985f52018-11-29 18:16:17 -0500705 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500706}
707
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400708angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400709 VkSurfaceKHR surface,
710 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500711{
712 // We've already initialized a device, and can't re-create it unless it's never been used.
713 // TODO(jmadill): Handle the re-creation case if necessary.
714 if (mDevice != VK_NULL_HANDLE)
715 {
716 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
717
718 // Check if the current device supports present on this surface.
719 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400720 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400721 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500722 surface, &supportsPresent));
723
Jamie Madill6cad7732018-07-11 09:01:17 -0400724 if (supportsPresent == VK_TRUE)
725 {
726 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill7c985f52018-11-29 18:16:17 -0500727 return angle::Result::Continue;
Jamie Madill6cad7732018-07-11 09:01:17 -0400728 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500729 }
730
731 // Find a graphics and present queue.
732 Optional<uint32_t> newPresentQueue;
733 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500734 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500735 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
736 {
737 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500738 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500739 {
740 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400741 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
742 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500743
744 if (supportsPresent == VK_TRUE)
745 {
746 newPresentQueue = queueIndex;
747 break;
748 }
749 }
750 }
751
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400752 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
753 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500754
Jamie Madill6cad7732018-07-11 09:01:17 -0400755 *presentQueueOut = newPresentQueue.value();
Jamie Madill7c985f52018-11-29 18:16:17 -0500756 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500757}
758
759std::string RendererVk::getVendorString() const
760{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300761 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500762}
763
Jamie Madille09bd5d2016-11-29 16:20:35 -0500764std::string RendererVk::getRendererDescription() const
765{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500766 std::stringstream strstr;
767
768 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
769
770 strstr << "Vulkan ";
771 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
772 strstr << VK_VERSION_MINOR(apiVersion) << ".";
773 strstr << VK_VERSION_PATCH(apiVersion);
774
Olli Etuahoc6a06182018-04-13 14:11:46 +0300775 strstr << "(";
776
777 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
778 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
779 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
780 // driver detection.
781 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
782 {
783 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
784 }
785
Geoff Langa7af56b2018-12-14 14:20:28 -0500786 strstr << mPhysicalDeviceProperties.deviceName;
787 strstr << " (" << gl::FmtHex(mPhysicalDeviceProperties.deviceID) << ")";
788
789 strstr << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500790
791 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500792}
793
Shahbaz Youssefi092481a2018-11-08 00:25:50 -0500794gl::Version RendererVk::getMaxSupportedESVersion() const
795{
796 // Declare GLES2 support if necessary features for GLES3 are missing
797 bool necessaryFeaturesForES3 = mPhysicalDeviceFeatures.inheritedQueries;
798
799 if (!necessaryFeaturesForES3)
800 {
801 return gl::Version(2, 0);
802 }
803
804 return gl::Version(3, 0);
805}
806
Ian Elliottbcb78902018-12-19 11:46:29 -0700807void RendererVk::initFeatures(const std::vector<VkExtensionProperties> &deviceExtensionProps)
Jamie Madill12222072018-07-11 14:59:48 -0400808{
Jamie Madillb36a4812018-09-25 10:15:11 -0400809// Use OpenGL line rasterization rules by default.
810// TODO(jmadill): Fix Android support. http://anglebug.com/2830
811#if defined(ANGLE_PLATFORM_ANDROID)
812 mFeatures.basicGLLineRasterization = false;
813#else
Jamie Madill12222072018-07-11 14:59:48 -0400814 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400815#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400816
Ian Elliott52f5da42018-12-21 09:02:09 -0700817 if ((mPhysicalDeviceProperties.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) ||
818 ExtensionFound(VK_KHR_MAINTENANCE1_EXTENSION_NAME, deviceExtensionProps))
Ian Elliottd50521f2018-12-20 12:05:14 -0700819 {
820 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
821 // investigation. http://anglebug.com/2728
822 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
823 }
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400824
825#ifdef ANGLE_PLATFORM_WINDOWS
826 // http://anglebug.com/2838
827 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
Shahbaz Youssefi4f3b2072019-01-01 14:48:25 -0500828
829 // http://anglebug.com/3055
830 mFeatures.forceCpuPathForCubeMapCopy = IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400831#endif
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400832
833 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
834 platform->overrideFeaturesVk(platform, &mFeatures);
Jamie Madillfde74c02018-11-18 16:12:02 -0500835
836 // Work around incorrect NVIDIA point size range clamping.
837 // TODO(jmadill): Narrow driver range once fixed. http://anglebug.com/2970
838 if (IsNvidia(mPhysicalDeviceProperties.vendorID))
839 {
840 mFeatures.clampPointSize = true;
841 }
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +0100842
843#if defined(ANGLE_PLATFORM_ANDROID)
Shahbaz Youssefib08457d2018-12-11 15:13:54 -0500844 // Work around ineffective compute-graphics barriers on Nexus 5X.
845 // TODO(syoussefi): Figure out which other vendors and driver versions are affected.
846 // http://anglebug.com/3019
847 mFeatures.flushAfterVertexConversion =
848 IsNexus5X(mPhysicalDeviceProperties.vendorID, mPhysicalDeviceProperties.deviceID);
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +0100849#endif
Ian Elliottbcb78902018-12-19 11:46:29 -0700850
851 if (ExtensionFound(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, deviceExtensionProps))
852 {
853 mFeatures.supportsIncrementalPresent = true;
854 }
Jamie Madill12222072018-07-11 14:59:48 -0400855}
856
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400857void RendererVk::initPipelineCacheVkKey()
858{
859 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
860 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
861 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
862 // there is no '\0' in the result.
863 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
864 {
865 hashStream << std::hex << c;
866 }
867 // Add the vendor and device id too for good measure.
868 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
869 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
870
871 const std::string &hashString = hashStream.str();
872 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
873 hashString.length(), mPipelineCacheVkBlobKey.data());
874}
875
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500876angle::Result RendererVk::initPipelineCache(DisplayVk *display)
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400877{
878 initPipelineCacheVkKey();
879
880 egl::BlobCache::Value initialData;
881 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
882 mPipelineCacheVkBlobKey, &initialData);
883
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400884 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400885
886 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400887 pipelineCacheCreateInfo.flags = 0;
888 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
889 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
890
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500891 ANGLE_VK_TRY(display, mPipelineCache.init(mDevice, pipelineCacheCreateInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -0500892 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400893}
894
Jamie Madillacccc6c2016-05-03 17:22:10 -0400895void RendererVk::ensureCapsInitialized() const
896{
897 if (!mCapsInitialized)
898 {
Shahbaz Youssefic2b576d2018-10-12 14:45:34 -0400899 ASSERT(mCurrentQueueFamilyIndex < mQueueFamilyProperties.size());
900 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures,
901 mQueueFamilyProperties[mCurrentQueueFamilyIndex], mNativeTextureCaps,
Jamie Madill30b5d842018-08-31 17:19:12 -0400902 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400903 mCapsInitialized = true;
904 }
905}
906
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400907void RendererVk::getSubmitWaitSemaphores(
908 vk::Context *context,
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400909 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores,
910 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> *waitStageMasks)
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400911{
912 if (mSubmitLastSignaledSemaphore.getSemaphore())
913 {
914 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400915 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400916
917 // Return the semaphore to the pool (which will remain valid and unused until the
918 // queue it's about to be waited on has finished execution).
919 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
920 }
921
922 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
923 {
924 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400925 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
926
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400927 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
928 }
929 mSubmitWaitSemaphores.clear();
930}
931
Jamie Madillacccc6c2016-05-03 17:22:10 -0400932const gl::Caps &RendererVk::getNativeCaps() const
933{
934 ensureCapsInitialized();
935 return mNativeCaps;
936}
937
938const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
939{
940 ensureCapsInitialized();
941 return mNativeTextureCaps;
942}
943
944const gl::Extensions &RendererVk::getNativeExtensions() const
945{
946 ensureCapsInitialized();
947 return mNativeExtensions;
948}
949
950const gl::Limitations &RendererVk::getNativeLimitations() const
951{
952 ensureCapsInitialized();
953 return mNativeLimitations;
954}
955
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400956uint32_t RendererVk::getMaxActiveTextures()
957{
958 // TODO(lucferron): expose this limitation to GL in Context Caps
959 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
960 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
961}
962
Jamie Madill49ac74b2017-12-21 14:42:33 -0500963const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500964{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500965 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500966}
967
Jamie Madill21061022018-07-12 23:56:30 -0400968angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500969{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500970 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500971 {
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400972 TRACE_EVENT0("gpu.angle", "RendererVk::finish");
973
Luc Ferron1617e692018-07-11 11:08:19 -0400974 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
975 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400976
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400977 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400978 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
979 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400980
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400981 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500982 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400983 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
984 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400985 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500986 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400987 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500988 submitInfo.signalSemaphoreCount = 0;
989 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500990
Jamie Madill21061022018-07-12 23:56:30 -0400991 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500992 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500993
Jamie Madill4c26fc22017-02-24 11:04:10 -0500994 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400995 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400996 freeAllInFlightResources();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400997
998 if (mGpuEventsEnabled)
999 {
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001000 // This loop should in practice execute once since the queue is already idle.
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001001 while (mInFlightGpuEventQueries.size() > 0)
1002 {
1003 ANGLE_TRY(checkCompletedGpuEvents(context));
1004 }
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001005 // Recalculate the CPU/GPU time difference to account for clock drifting. Avoid unnecessary
1006 // synchronization if there is no event to be adjusted (happens when finish() gets called
1007 // multiple times towards the end of the application).
1008 if (mGpuEvents.size() > 0)
1009 {
1010 ANGLE_TRY(synchronizeCpuGpuTime(context));
1011 }
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001012 }
1013
Jamie Madill7c985f52018-11-29 18:16:17 -05001014 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001015}
1016
Jamie Madill0c0dc342017-03-24 14:18:51 -04001017void RendererVk::freeAllInFlightResources()
1018{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001019 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -04001020 {
Yuly Novikovb56ddbb2018-11-02 16:53:18 -04001021 // On device loss we need to wait for fence to be signaled before destroying it
1022 if (mDeviceLost)
1023 {
1024 VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs);
1025 // If wait times out, it is probably not possible to recover from lost device
1026 ASSERT(status == VK_SUCCESS || status == VK_ERROR_DEVICE_LOST);
1027 }
Jamie Madill49ac74b2017-12-21 14:42:33 -05001028 batch.fence.destroy(mDevice);
1029 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001030 }
1031 mInFlightCommands.clear();
1032
1033 for (auto &garbage : mGarbage)
1034 {
Jamie Madille88ec8e2017-10-31 17:18:14 -04001035 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001036 }
1037 mGarbage.clear();
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001038
1039 mLastCompletedQueueSerial = mLastSubmittedQueueSerial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001040}
1041
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001042angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001043{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001044 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -05001045
Jamie Madill49ac74b2017-12-21 14:42:33 -05001046 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001047 {
Yuly Novikov27780292018-11-09 11:19:49 -05001048 VkResult result = batch.fence.getStatus(mDevice);
1049 if (result == VK_NOT_READY)
1050 {
Jamie Madill0c0dc342017-03-24 14:18:51 -04001051 break;
Yuly Novikov27780292018-11-09 11:19:49 -05001052 }
1053 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001054
Jamie Madill49ac74b2017-12-21 14:42:33 -05001055 ASSERT(batch.serial > mLastCompletedQueueSerial);
1056 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001057
Jamie Madill49ac74b2017-12-21 14:42:33 -05001058 batch.fence.destroy(mDevice);
1059 batch.commandPool.destroy(mDevice);
1060 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001061 }
1062
Jamie Madill49ac74b2017-12-21 14:42:33 -05001063 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001064
1065 size_t freeIndex = 0;
1066 for (; freeIndex < mGarbage.size(); ++freeIndex)
1067 {
Jamie Madill49ac74b2017-12-21 14:42:33 -05001068 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -04001069 break;
1070 }
1071
1072 // Remove the entries from the garbage list - they should be ready to go.
1073 if (freeIndex > 0)
1074 {
1075 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -05001076 }
1077
Jamie Madill7c985f52018-11-29 18:16:17 -05001078 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001079}
1080
Jamie Madill21061022018-07-12 23:56:30 -04001081angle::Result RendererVk::submitFrame(vk::Context *context,
1082 const VkSubmitInfo &submitInfo,
1083 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001084{
Tobin Ehlis573f76b2018-05-03 11:10:44 -06001085 TRACE_EVENT0("gpu.angle", "RendererVk::submitFrame");
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001086 VkFenceCreateInfo fenceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -05001087 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1088 fenceInfo.flags = 0;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001089
Jamie Madillbea35a62018-07-05 11:54:10 -04001090 vk::Scoped<CommandBatch> scopedBatch(mDevice);
1091 CommandBatch &batch = scopedBatch.get();
Yuly Novikov27780292018-11-09 11:19:49 -05001092 ANGLE_VK_TRY(context, batch.fence.init(mDevice, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001093
Jamie Madill21061022018-07-12 23:56:30 -04001094 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -05001095
1096 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -05001097 batch.commandPool = std::move(mCommandPool);
1098 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001099
Jamie Madillbea35a62018-07-05 11:54:10 -04001100 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -04001101
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001102 // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on
1103 // swap() though, and there could be multiple submissions in between (through glFlush() calls),
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001104 // so the limit is larger than the expected number of images. The
1105 // InterleavedAttributeDataBenchmark perf test for example issues a large number of flushes.
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001106 ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001107
1108 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -04001109 // TODO(jmadill): Overflow check.
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001110 mLastSubmittedQueueSerial = mCurrentQueueSerial;
Jamie Madillb980c562018-11-27 11:34:27 -05001111 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -04001112
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001113 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001114
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001115 if (mGpuEventsEnabled)
1116 {
1117 ANGLE_TRY(checkCompletedGpuEvents(context));
1118 }
1119
Jamie Madill49ac74b2017-12-21 14:42:33 -05001120 // Simply null out the command buffer here - it was allocated using the command pool.
1121 commandBuffer.releaseHandle();
1122
1123 // Reallocate the command pool for next frame.
1124 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001125 VkCommandPoolCreateInfo poolInfo = {};
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001126 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001127 poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001128 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001129
Yuly Novikov27780292018-11-09 11:19:49 -05001130 ANGLE_VK_TRY(context, mCommandPool.init(mDevice, poolInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -05001131 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001132}
1133
Jamie Madillaaca96e2018-06-12 10:19:48 -04001134bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -05001135{
1136 return serial > mLastCompletedQueueSerial;
1137}
1138
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001139angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
1140{
1141 if (!isSerialInUse(serial) || mInFlightCommands.empty())
1142 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001143 return angle::Result::Continue;
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001144 }
1145
1146 // Find the first batch with serial equal to or bigger than given serial (note that
1147 // the batch serials are unique, otherwise upper-bound would have been necessary).
1148 size_t batchIndex = mInFlightCommands.size() - 1;
1149 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
1150 {
1151 if (mInFlightCommands[i].serial >= serial)
1152 {
1153 batchIndex = i;
1154 break;
1155 }
1156 }
1157 const CommandBatch &batch = mInFlightCommands[batchIndex];
1158
1159 // Wait for it finish
Yuly Novikov27780292018-11-09 11:19:49 -05001160 ANGLE_VK_TRY(context, batch.fence.wait(mDevice, kMaxFenceWaitTimeNs));
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001161
1162 // Clean up finished batches.
1163 return checkCompletedCommands(context);
1164}
1165
Jamie Madill21061022018-07-12 23:56:30 -04001166angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1167 const vk::RenderPassDesc &desc,
1168 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001169{
Jamie Madill21061022018-07-12 23:56:30 -04001170 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001171 renderPassOut);
1172}
1173
Jamie Madill21061022018-07-12 23:56:30 -04001174angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1175 const vk::RenderPassDesc &desc,
1176 const vk::AttachmentOpsArray &ops,
1177 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001178{
Jamie Madill21061022018-07-12 23:56:30 -04001179 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001180 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001181}
1182
Jamie Madilla5e06072018-05-18 14:36:05 -04001183vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001184{
Jamie Madilla5e06072018-05-18 14:36:05 -04001185 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001186}
1187
Jamie Madill21061022018-07-12 23:56:30 -04001188angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001189{
Jamie Madill21061022018-07-12 23:56:30 -04001190 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001191 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001192}
1193
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001194angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001195{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001196 if (mCommandGraph.empty())
1197 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001198 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001199 }
1200
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001201 TRACE_EVENT0("gpu.angle", "RendererVk::flush");
1202
Jamie Madillbea35a62018-07-05 11:54:10 -04001203 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1204 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001205
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001206 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001207 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1208 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001209
1210 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1211 // will be waited on.
1212 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001213
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001214 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001215 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001216 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1217 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001218 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001219 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001220 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001221 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001222 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001223
Jamie Madill21061022018-07-12 23:56:30 -04001224 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001225
Jamie Madill7c985f52018-11-29 18:16:17 -05001226 return angle::Result::Continue;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001227}
1228
Jamie Madill78feddc2018-04-27 11:45:05 -04001229Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001230{
Jamie Madill78feddc2018-04-27 11:45:05 -04001231 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001232}
1233
Jamie Madill21061022018-07-12 23:56:30 -04001234angle::Result RendererVk::getDescriptorSetLayout(
1235 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001236 const vk::DescriptorSetLayoutDesc &desc,
1237 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1238{
Jamie Madill21061022018-07-12 23:56:30 -04001239 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001240}
1241
Jamie Madill21061022018-07-12 23:56:30 -04001242angle::Result RendererVk::getPipelineLayout(
1243 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001244 const vk::PipelineLayoutDesc &desc,
1245 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1246 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1247{
Jamie Madill21061022018-07-12 23:56:30 -04001248 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001249 pipelineLayoutOut);
1250}
1251
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001252angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1253{
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001254 ASSERT(mPipelineCache.valid());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001255
1256 if (--mPipelineCacheVkUpdateTimeout > 0)
1257 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001258 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001259 }
1260
1261 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1262
1263 // Get the size of the cache.
1264 size_t pipelineCacheSize = 0;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001265 VkResult result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, nullptr);
Yuly Novikov27780292018-11-09 11:19:49 -05001266 if (result != VK_INCOMPLETE)
1267 {
1268 ANGLE_VK_TRY(displayVk, result);
1269 }
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001270
1271 angle::MemoryBuffer *pipelineCacheData = nullptr;
1272 ANGLE_VK_CHECK_ALLOC(displayVk,
1273 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1274
1275 size_t originalPipelineCacheSize = pipelineCacheSize;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001276 result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, pipelineCacheData->data());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001277 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1278 // was determined just above), so receiving it hints at an implementation bug we would want
1279 // to know about early.
Yuly Novikov27780292018-11-09 11:19:49 -05001280 ASSERT(result != VK_INCOMPLETE);
1281 ANGLE_VK_TRY(displayVk, result);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001282
1283 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1284 // the buffer to avoid leaking garbage memory.
1285 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1286 if (pipelineCacheSize < originalPipelineCacheSize)
1287 {
1288 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1289 originalPipelineCacheSize - pipelineCacheSize);
1290 }
1291
1292 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1293
Jamie Madill7c985f52018-11-29 18:16:17 -05001294 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001295}
1296
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001297angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1298 const vk::Semaphore **outSemaphore)
1299{
1300 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1301
1302 vk::SemaphoreHelper semaphore;
1303 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1304
1305 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1306 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1307
Jamie Madill7c985f52018-11-29 18:16:17 -05001308 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001309}
1310
1311const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1312{
1313 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1314
1315 // Return the semaphore to the pool (which will remain valid and unused until the
1316 // queue it's about to be waited on has finished execution). The caller is about
1317 // to wait on it.
1318 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1319
1320 return semaphore;
1321}
1322
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001323angle::Result RendererVk::getTimestamp(vk::Context *context, uint64_t *timestampOut)
1324{
1325 // The intent of this function is to query the timestamp without stalling the GPU. Currently,
1326 // that seems impossible, so instead, we are going to make a small submission with just a
1327 // timestamp query. First, the disjoint timer query extension says:
1328 //
1329 // > This will return the GL time after all previous commands have reached the GL server but
1330 // have not yet necessarily executed.
1331 //
1332 // The previous commands are stored in the command graph at the moment and are not yet flushed.
1333 // The wording allows us to make a submission to get the timestamp without performing a flush.
1334 //
1335 // Second:
1336 //
1337 // > By using a combination of this synchronous get command and the asynchronous timestamp query
1338 // object target, applications can measure the latency between when commands reach the GL server
1339 // and when they are realized in the framebuffer.
1340 //
1341 // This fits with the above strategy as well, although inevitably we are possibly introducing a
1342 // GPU bubble. This function directly generates a command buffer and submits it instead of
1343 // using the other member functions. This is to avoid changing any state, such as the queue
1344 // serial.
1345
1346 // Create a query used to receive the GPU timestamp
1347 vk::Scoped<vk::DynamicQueryPool> timestampQueryPool(mDevice);
1348 vk::QueryHelper timestampQuery;
1349 ANGLE_TRY(timestampQueryPool.get().init(context, VK_QUERY_TYPE_TIMESTAMP, 1));
1350 ANGLE_TRY(timestampQueryPool.get().allocateQuery(context, &timestampQuery));
1351
1352 // Record the command buffer
1353 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1354 vk::CommandBuffer &commandBuffer = commandBatch.get();
1355
1356 VkCommandBufferAllocateInfo commandBufferInfo = {};
1357 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1358 commandBufferInfo.commandPool = mCommandPool.getHandle();
1359 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1360 commandBufferInfo.commandBufferCount = 1;
1361
Yuly Novikov27780292018-11-09 11:19:49 -05001362 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001363
1364 VkCommandBufferBeginInfo beginInfo = {};
1365 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1366 beginInfo.flags = 0;
1367 beginInfo.pInheritanceInfo = nullptr;
1368
Yuly Novikov27780292018-11-09 11:19:49 -05001369 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001370
1371 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1372 timestampQuery.getQuery(), 1);
1373 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1374 timestampQuery.getQueryPool()->getHandle(),
1375 timestampQuery.getQuery());
1376
Yuly Novikov27780292018-11-09 11:19:49 -05001377 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001378
1379 // Create fence for the submission
1380 VkFenceCreateInfo fenceInfo = {};
1381 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1382 fenceInfo.flags = 0;
1383
1384 vk::Scoped<vk::Fence> fence(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001385 ANGLE_VK_TRY(context, fence.get().init(mDevice, fenceInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001386
1387 // Submit the command buffer
1388 VkSubmitInfo submitInfo = {};
1389 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1390 submitInfo.waitSemaphoreCount = 0;
1391 submitInfo.pWaitSemaphores = nullptr;
1392 submitInfo.pWaitDstStageMask = nullptr;
1393 submitInfo.commandBufferCount = 1;
1394 submitInfo.pCommandBuffers = commandBuffer.ptr();
1395 submitInfo.signalSemaphoreCount = 0;
1396 submitInfo.pSignalSemaphores = nullptr;
1397
1398 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, fence.get().getHandle()));
1399
1400 // Wait for the submission to finish. Given no semaphores, there is hope that it would execute
1401 // in parallel with what's already running on the GPU.
Yuly Novikov27780292018-11-09 11:19:49 -05001402 ANGLE_VK_TRY(context, fence.get().wait(mDevice, kMaxFenceWaitTimeNs));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001403
1404 // Get the query results
1405 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1406
Yuly Novikov27780292018-11-09 11:19:49 -05001407 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
1408 mDevice, timestampQuery.getQuery(), 1, sizeof(*timestampOut),
1409 timestampOut, sizeof(*timestampOut), queryFlags));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001410
1411 timestampQueryPool.get().freeQuery(context, &timestampQuery);
1412
Jamie Madill7c985f52018-11-29 18:16:17 -05001413 return angle::Result::Continue;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001414}
1415
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05001416// These functions look at the mandatory format for support, and fallback to querying the device (if
1417// necessary) to test the availability of the bits.
1418bool RendererVk::hasLinearTextureFormatFeatureBits(VkFormat format,
1419 const VkFormatFeatureFlags featureBits)
1420{
1421 return hasFormatFeatureBits<&VkFormatProperties::linearTilingFeatures>(format, featureBits);
1422}
1423
1424bool RendererVk::hasTextureFormatFeatureBits(VkFormat format,
1425 const VkFormatFeatureFlags featureBits)
1426{
1427 return hasFormatFeatureBits<&VkFormatProperties::optimalTilingFeatures>(format, featureBits);
1428}
1429
1430bool RendererVk::hasBufferFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
1431{
1432 return hasFormatFeatureBits<&VkFormatProperties::bufferFeatures>(format, featureBits);
1433}
1434
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001435angle::Result RendererVk::synchronizeCpuGpuTime(vk::Context *context)
1436{
1437 ASSERT(mGpuEventsEnabled);
1438
1439 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1440 ASSERT(platform);
1441
1442 // To synchronize CPU and GPU times, we need to get the CPU timestamp as close as possible to
1443 // the GPU timestamp. The process of getting the GPU timestamp is as follows:
1444 //
1445 // CPU GPU
1446 //
1447 // Record command buffer
1448 // with timestamp query
1449 //
1450 // Submit command buffer
1451 //
1452 // Post-submission work Begin execution
1453 //
1454 // ???? Write timstamp Tgpu
1455 //
1456 // ???? End execution
1457 //
1458 // ???? Return query results
1459 //
1460 // ????
1461 //
1462 // Get query results
1463 //
1464 // The areas of unknown work (????) on the CPU indicate that the CPU may or may not have
1465 // finished post-submission work while the GPU is executing in parallel. With no further work,
1466 // querying CPU timestamps before submission and after getting query results give the bounds to
1467 // Tgpu, which could be quite large.
1468 //
1469 // Using VkEvents, the GPU can be made to wait for the CPU and vice versa, in an effort to
1470 // reduce this range. This function implements the following procedure:
1471 //
1472 // CPU GPU
1473 //
1474 // Record command buffer
1475 // with timestamp query
1476 //
1477 // Submit command buffer
1478 //
1479 // Post-submission work Begin execution
1480 //
1481 // ???? Set Event GPUReady
1482 //
1483 // Wait on Event GPUReady Wait on Event CPUReady
1484 //
1485 // Get CPU Time Ts Wait on Event CPUReady
1486 //
1487 // Set Event CPUReady Wait on Event CPUReady
1488 //
1489 // Get CPU Time Tcpu Get GPU Time Tgpu
1490 //
1491 // Wait on Event GPUDone Set Event GPUDone
1492 //
1493 // Get CPU Time Te End Execution
1494 //
1495 // Idle Return query results
1496 //
1497 // Get query results
1498 //
1499 // If Te-Ts > epsilon, a GPU or CPU interruption can be assumed and the operation can be
1500 // retried. Once Te-Ts < epsilon, Tcpu can be taken to presumably match Tgpu. Finding an
1501 // epsilon that's valid for all devices may be difficult, so the loop can be performed only a
1502 // limited number of times and the Tcpu,Tgpu pair corresponding to smallest Te-Ts used for
1503 // calibration.
1504 //
1505 // Note: Once VK_EXT_calibrated_timestamps is ubiquitous, this should be redone.
1506
1507 // Make sure nothing is running
1508 ASSERT(mCommandGraph.empty());
1509
1510 TRACE_EVENT0("gpu.angle", "RendererVk::synchronizeCpuGpuTime");
1511
1512 // Create a query used to receive the GPU timestamp
1513 vk::QueryHelper timestampQuery;
1514 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &timestampQuery));
1515
1516 // Create the three events
1517 VkEventCreateInfo eventCreateInfo = {};
1518 eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
1519 eventCreateInfo.flags = 0;
1520
1521 vk::Scoped<vk::Event> cpuReady(mDevice), gpuReady(mDevice), gpuDone(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001522 ANGLE_VK_TRY(context, cpuReady.get().init(mDevice, eventCreateInfo));
1523 ANGLE_VK_TRY(context, gpuReady.get().init(mDevice, eventCreateInfo));
1524 ANGLE_VK_TRY(context, gpuDone.get().init(mDevice, eventCreateInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001525
1526 constexpr uint32_t kRetries = 10;
1527
1528 // Time suffixes used are S for seconds and Cycles for cycles
1529 double tightestRangeS = 1e6f;
1530 double TcpuS = 0;
1531 uint64_t TgpuCycles = 0;
1532 for (uint32_t i = 0; i < kRetries; ++i)
1533 {
1534 // Reset the events
Yuly Novikov27780292018-11-09 11:19:49 -05001535 ANGLE_VK_TRY(context, cpuReady.get().reset(mDevice));
1536 ANGLE_VK_TRY(context, gpuReady.get().reset(mDevice));
1537 ANGLE_VK_TRY(context, gpuDone.get().reset(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001538
1539 // Record the command buffer
1540 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1541 vk::CommandBuffer &commandBuffer = commandBatch.get();
1542
1543 VkCommandBufferAllocateInfo commandBufferInfo = {};
1544 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1545 commandBufferInfo.commandPool = mCommandPool.getHandle();
1546 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1547 commandBufferInfo.commandBufferCount = 1;
1548
Yuly Novikov27780292018-11-09 11:19:49 -05001549 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001550
1551 VkCommandBufferBeginInfo beginInfo = {};
1552 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1553 beginInfo.flags = 0;
1554 beginInfo.pInheritanceInfo = nullptr;
1555
Yuly Novikov27780292018-11-09 11:19:49 -05001556 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001557
1558 commandBuffer.setEvent(gpuReady.get(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
1559 commandBuffer.waitEvents(1, cpuReady.get().ptr(), VK_PIPELINE_STAGE_HOST_BIT,
1560 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, nullptr, 0, nullptr, 0,
1561 nullptr);
1562
1563 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1564 timestampQuery.getQuery(), 1);
1565 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1566 timestampQuery.getQueryPool()->getHandle(),
1567 timestampQuery.getQuery());
1568
1569 commandBuffer.setEvent(gpuDone.get(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
1570
Yuly Novikov27780292018-11-09 11:19:49 -05001571 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001572
1573 // Submit the command buffer
1574 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1575 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1576 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
1577
1578 VkSubmitInfo submitInfo = {};
1579 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1580 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1581 submitInfo.pWaitSemaphores = waitSemaphores.data();
1582 submitInfo.pWaitDstStageMask = waitStageMasks.data();
1583 submitInfo.commandBufferCount = 1;
1584 submitInfo.pCommandBuffers = commandBuffer.ptr();
1585 submitInfo.signalSemaphoreCount = 0;
1586 submitInfo.pSignalSemaphores = nullptr;
1587
1588 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBuffer)));
1589
1590 // Wait for GPU to be ready. This is a short busy wait.
Yuly Novikov27780292018-11-09 11:19:49 -05001591 VkResult result = VK_EVENT_RESET;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001592 do
1593 {
Yuly Novikov27780292018-11-09 11:19:49 -05001594 result = gpuReady.get().getStatus(mDevice);
1595 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
1596 {
1597 ANGLE_VK_TRY(context, result);
1598 }
1599 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001600
1601 double TsS = platform->monotonicallyIncreasingTime(platform);
1602
1603 // Tell the GPU to go ahead with the timestamp query.
Yuly Novikov27780292018-11-09 11:19:49 -05001604 ANGLE_VK_TRY(context, cpuReady.get().set(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001605 double cpuTimestampS = platform->monotonicallyIncreasingTime(platform);
1606
1607 // Wait for GPU to be done. Another short busy wait.
1608 do
1609 {
Yuly Novikov27780292018-11-09 11:19:49 -05001610 result = gpuDone.get().getStatus(mDevice);
1611 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
1612 {
1613 ANGLE_VK_TRY(context, result);
1614 }
1615 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001616
1617 double TeS = platform->monotonicallyIncreasingTime(platform);
1618
1619 // Get the query results
1620 ANGLE_TRY(finishToSerial(context, getLastSubmittedQueueSerial()));
1621
1622 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1623
1624 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05001625 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
1626 mDevice, timestampQuery.getQuery(), 1, sizeof(gpuTimestampCycles),
1627 &gpuTimestampCycles, sizeof(gpuTimestampCycles), queryFlags));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001628
1629 // Use the first timestamp queried as origin.
1630 if (mGpuEventTimestampOrigin == 0)
1631 {
1632 mGpuEventTimestampOrigin = gpuTimestampCycles;
1633 }
1634
1635 // Take these CPU and GPU timestamps if there is better confidence.
1636 double confidenceRangeS = TeS - TsS;
1637 if (confidenceRangeS < tightestRangeS)
1638 {
1639 tightestRangeS = confidenceRangeS;
1640 TcpuS = cpuTimestampS;
1641 TgpuCycles = gpuTimestampCycles;
1642 }
1643 }
1644
1645 mGpuEventQueryPool.freeQuery(context, &timestampQuery);
1646
1647 // timestampPeriod gives nanoseconds/cycle.
1648 double TgpuS = (TgpuCycles - mGpuEventTimestampOrigin) *
1649 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) /
1650 1'000'000'000.0;
1651
1652 flushGpuEvents(TgpuS, TcpuS);
1653
1654 mGpuClockSync.gpuTimestampS = TgpuS;
1655 mGpuClockSync.cpuTimestampS = TcpuS;
1656
Jamie Madill7c985f52018-11-29 18:16:17 -05001657 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001658}
1659
1660angle::Result RendererVk::traceGpuEventImpl(vk::Context *context,
1661 vk::CommandBuffer *commandBuffer,
1662 char phase,
1663 const char *name)
1664{
1665 ASSERT(mGpuEventsEnabled);
1666
1667 GpuEventQuery event;
1668
1669 event.name = name;
1670 event.phase = phase;
1671 event.serial = mCurrentQueueSerial;
1672
1673 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &event.queryPoolIndex, &event.queryIndex));
1674
1675 commandBuffer->resetQueryPool(
1676 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex, 1);
1677 commandBuffer->writeTimestamp(
1678 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1679 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex);
1680
1681 mInFlightGpuEventQueries.push_back(std::move(event));
1682
Jamie Madill7c985f52018-11-29 18:16:17 -05001683 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001684}
1685
1686angle::Result RendererVk::checkCompletedGpuEvents(vk::Context *context)
1687{
1688 ASSERT(mGpuEventsEnabled);
1689
1690 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1691 ASSERT(platform);
1692
1693 int finishedCount = 0;
1694
1695 for (GpuEventQuery &eventQuery : mInFlightGpuEventQueries)
1696 {
1697 // Only check the timestamp query if the submission has finished.
1698 if (eventQuery.serial > mLastCompletedQueueSerial)
1699 {
1700 break;
1701 }
1702
1703 // See if the results are available.
1704 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05001705 VkResult result = mGpuEventQueryPool.getQueryPool(eventQuery.queryPoolIndex)
1706 ->getResults(mDevice, eventQuery.queryIndex, 1,
1707 sizeof(gpuTimestampCycles), &gpuTimestampCycles,
1708 sizeof(gpuTimestampCycles), VK_QUERY_RESULT_64_BIT);
1709 if (result == VK_NOT_READY)
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001710 {
1711 break;
1712 }
Yuly Novikov27780292018-11-09 11:19:49 -05001713 ANGLE_VK_TRY(context, result);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001714
1715 mGpuEventQueryPool.freeQuery(context, eventQuery.queryPoolIndex, eventQuery.queryIndex);
1716
1717 GpuEvent event;
1718 event.gpuTimestampCycles = gpuTimestampCycles;
1719 event.name = eventQuery.name;
1720 event.phase = eventQuery.phase;
1721
1722 mGpuEvents.emplace_back(event);
1723
1724 ++finishedCount;
1725 }
1726
1727 mInFlightGpuEventQueries.erase(mInFlightGpuEventQueries.begin(),
1728 mInFlightGpuEventQueries.begin() + finishedCount);
1729
Jamie Madill7c985f52018-11-29 18:16:17 -05001730 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001731}
1732
1733void RendererVk::flushGpuEvents(double nextSyncGpuTimestampS, double nextSyncCpuTimestampS)
1734{
1735 if (mGpuEvents.size() == 0)
1736 {
1737 return;
1738 }
1739
1740 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1741 ASSERT(platform);
1742
1743 // Find the slope of the clock drift for adjustment
1744 double lastGpuSyncTimeS = mGpuClockSync.gpuTimestampS;
1745 double lastGpuSyncDiffS = mGpuClockSync.cpuTimestampS - mGpuClockSync.gpuTimestampS;
1746 double gpuSyncDriftSlope = 0;
1747
1748 double nextGpuSyncTimeS = nextSyncGpuTimestampS;
1749 double nextGpuSyncDiffS = nextSyncCpuTimestampS - nextSyncGpuTimestampS;
1750
1751 // No gpu trace events should have been generated before the clock sync, so if there is no
1752 // "previous" clock sync, there should be no gpu events (i.e. the function early-outs above).
1753 ASSERT(mGpuClockSync.gpuTimestampS != std::numeric_limits<double>::max() &&
1754 mGpuClockSync.cpuTimestampS != std::numeric_limits<double>::max());
1755
1756 gpuSyncDriftSlope =
1757 (nextGpuSyncDiffS - lastGpuSyncDiffS) / (nextGpuSyncTimeS - lastGpuSyncTimeS);
1758
1759 for (const GpuEvent &event : mGpuEvents)
1760 {
1761 double gpuTimestampS =
1762 (event.gpuTimestampCycles - mGpuEventTimestampOrigin) *
1763 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) * 1e-9;
1764
1765 // Account for clock drift.
1766 gpuTimestampS += lastGpuSyncDiffS + gpuSyncDriftSlope * (gpuTimestampS - lastGpuSyncTimeS);
1767
1768 // Generate the trace now that the GPU timestamp is available and clock drifts are accounted
1769 // for.
1770 static long long eventId = 1;
1771 static const unsigned char *categoryEnabled =
1772 TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu.angle.gpu");
1773 platform->addTraceEvent(platform, event.phase, categoryEnabled, event.name, eventId++,
1774 gpuTimestampS, 0, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
1775 }
1776
1777 mGpuEvents.clear();
1778}
1779
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05001780template <VkFormatFeatureFlags VkFormatProperties::*features>
1781bool RendererVk::hasFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
1782{
1783 ASSERT(static_cast<uint32_t>(format) < vk::kNumVkFormats);
1784 VkFormatProperties &deviceProperties = mFormatProperties[format];
1785
1786 if (deviceProperties.bufferFeatures == kInvalidFormatFeatureFlags)
1787 {
1788 // If we don't have the actual device features, see if the requested features are mandatory.
1789 // If so, there's no need to query the device.
1790 const VkFormatProperties &mandatoryProperties = vk::GetMandatoryFormatSupport(format);
1791 if (IsMaskFlagSet(mandatoryProperties.*features, featureBits))
1792 {
1793 return true;
1794 }
1795
1796 // Otherwise query the format features and cache it.
1797 vkGetPhysicalDeviceFormatProperties(mPhysicalDevice, format, &deviceProperties);
1798 }
1799
1800 return IsMaskFlagSet(deviceProperties.*features, featureBits);
1801}
1802
Jamie Madillaaca96e2018-06-12 10:19:48 -04001803uint32_t GetUniformBufferDescriptorCount()
1804{
1805 return kUniformBufferDescriptorsPerDescriptorSet;
1806}
1807
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001808} // namespace rx