blob: 458945132ac6226cd23ff42028f25aec593e680d [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"
Yuly Novikov5fe7c5b2019-01-17 12:16:34 -050018#include "common/platform.h"
Jamie Madilla66779f2017-01-06 10:43:44 -050019#include "common/system_utils.h"
Jamie Madill85ca1892019-01-16 13:27:15 -050020#include "libANGLE/Context.h"
Yuly Novikovb56ddbb2018-11-02 16:53:18 -040021#include "libANGLE/Display.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050022#include "libANGLE/renderer/driver_utils.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050023#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050024#include "libANGLE/renderer/vulkan/CompilerVk.h"
Jamie Madill85ca1892019-01-16 13:27:15 -050025#include "libANGLE/renderer/vulkan/ContextVk.h"
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040026#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050027#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050028#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050029#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050030#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050031#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050032#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050033#include "platform/Platform.h"
Shahbaz Youssefi61656022018-10-24 15:00:50 -040034#include "third_party/trace_event/trace_event.h"
35
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070036// Consts
37namespace
38{
Jamie Madill7c985f52018-11-29 18:16:17 -050039const uint32_t kMockVendorID = 0xba5eba11;
40const uint32_t kMockDeviceID = 0xf005ba11;
41constexpr char kMockDeviceName[] = "Vulkan Mock Device";
42constexpr size_t kInFlightCommandsLimit = 100u;
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -050043constexpr VkFormatFeatureFlags kInvalidFormatFeatureFlags = static_cast<VkFormatFeatureFlags>(-1);
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070044} // anonymous namespace
45
Jamie Madill9e54b5a2016-05-25 12:57:39 -040046namespace rx
47{
48
Jamie Madille09bd5d2016-11-29 16:20:35 -050049namespace
50{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040051// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
52// one for the vertex shader.
53constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040054// Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes)
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -050055constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -040056// Wait a maximum of 10s. If that times out, we declare it a failure.
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -050057constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
Ian Elliott356d26c2019-02-11 11:27:01 -070058// Per the Vulkan specification, as long as Vulkan 1.1+ is returned by vkEnumerateInstanceVersion,
59// ANGLE must indicate the highest version of Vulkan functionality that it uses. The Vulkan
60// validation layers will issue messages for any core functionality that requires a higher version.
61// This value must be increased whenever ANGLE starts using functionality from a newer core
62// version of Vulkan.
63constexpr uint32_t kPreferredVulkanAPIVersion = VK_API_VERSION_1_1;
Jamie Madille09bd5d2016-11-29 16:20:35 -050064
Omar El Sheikh26c61b22018-06-29 12:50:59 -060065bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
66{
67#if !defined(ANGLE_PLATFORM_ANDROID)
68 // Mock ICD does not currently run on Android
69 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
70 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
71 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
72#else
73 return false;
74#endif // !defined(ANGLE_PLATFORM_ANDROID)
75}
76
Frank Henigman52047de2018-11-13 17:22:36 -050077bool StrLess(const char *a, const char *b)
Jamie Madille09bd5d2016-11-29 16:20:35 -050078{
Frank Henigman52047de2018-11-13 17:22:36 -050079 return strcmp(a, b) < 0;
Jamie Madille09bd5d2016-11-29 16:20:35 -050080}
81
Frank Henigman52047de2018-11-13 17:22:36 -050082VkResult VerifyExtensionsPresent(const RendererVk::ExtensionNameList &haystack,
83 const RendererVk::ExtensionNameList &needles)
Tobin Ehlis3a181e32018-08-29 15:17:05 -060084{
Frank Henigman52047de2018-11-13 17:22:36 -050085 // NOTE: The lists must be sorted.
86 return std::includes(haystack.begin(), haystack.end(), needles.begin(), needles.end(), StrLess)
87 ? VK_SUCCESS
88 : VK_ERROR_EXTENSION_NOT_PRESENT;
89}
90
91bool ExtensionFound(const char *needle, const RendererVk::ExtensionNameList &haystack)
92{
93 // NOTE: The list must be sorted.
94 return std::binary_search(haystack.begin(), haystack.end(), needle, StrLess);
Tobin Ehlis3a181e32018-08-29 15:17:05 -060095}
96
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -050097// Array of Validation error/warning messages that will be ignored, should include bugID
Jamie Madill00f43c92019-02-09 11:41:12 -050098constexpr const char *kSkippedMessages[] = {
99 // http://anglebug.com/2866
100 "UNASSIGNED-CoreValidation-Shader-OutputNotConsumed",
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500101 // http://anglebug.com/2796
Jamie Madill00f43c92019-02-09 11:41:12 -0500102 "UNASSIGNED-CoreValidation-Shader-PointSizeMissing",
103};
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500104
105// Suppress validation errors that are known
106// return "true" if given code/prefix/message is known, else return "false"
107bool IsIgnoredDebugMessage(const char *message)
108{
Michael Spang25839802019-01-30 18:02:51 -0500109 if (!message)
110 {
111 return false;
112 }
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500113 for (const char *msg : kSkippedMessages)
114 {
115 if (strstr(message, msg) != nullptr)
116 {
117 return true;
118 }
119 }
120 return false;
121}
122
123const char *GetVkObjectTypeName(VkObjectType type)
124{
125 switch (type)
126 {
127 case VK_OBJECT_TYPE_UNKNOWN:
128 return "Unknown";
129 case VK_OBJECT_TYPE_INSTANCE:
130 return "Instance";
131 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
132 return "Physical Device";
133 case VK_OBJECT_TYPE_DEVICE:
134 return "Device";
135 case VK_OBJECT_TYPE_QUEUE:
136 return "Queue";
137 case VK_OBJECT_TYPE_SEMAPHORE:
138 return "Semaphore";
139 case VK_OBJECT_TYPE_COMMAND_BUFFER:
140 return "Command Buffer";
141 case VK_OBJECT_TYPE_FENCE:
142 return "Fence";
143 case VK_OBJECT_TYPE_DEVICE_MEMORY:
144 return "Device Memory";
145 case VK_OBJECT_TYPE_BUFFER:
146 return "Buffer";
147 case VK_OBJECT_TYPE_IMAGE:
148 return "Image";
149 case VK_OBJECT_TYPE_EVENT:
150 return "Event";
151 case VK_OBJECT_TYPE_QUERY_POOL:
152 return "Query Pool";
153 case VK_OBJECT_TYPE_BUFFER_VIEW:
154 return "Buffer View";
155 case VK_OBJECT_TYPE_IMAGE_VIEW:
156 return "Image View";
157 case VK_OBJECT_TYPE_SHADER_MODULE:
158 return "Shader Module";
159 case VK_OBJECT_TYPE_PIPELINE_CACHE:
160 return "Pipeline Cache";
161 case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
162 return "Pipeline Layout";
163 case VK_OBJECT_TYPE_RENDER_PASS:
164 return "Render Pass";
165 case VK_OBJECT_TYPE_PIPELINE:
166 return "Pipeline";
167 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
168 return "Descriptor Set Layout";
169 case VK_OBJECT_TYPE_SAMPLER:
170 return "Sampler";
171 case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
172 return "Descriptor Pool";
173 case VK_OBJECT_TYPE_DESCRIPTOR_SET:
174 return "Descriptor Set";
175 case VK_OBJECT_TYPE_FRAMEBUFFER:
176 return "Framebuffer";
177 case VK_OBJECT_TYPE_COMMAND_POOL:
178 return "Command Pool";
179 case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION:
180 return "Sampler YCbCr Conversion";
181 case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE:
182 return "Descriptor Update Template";
183 case VK_OBJECT_TYPE_SURFACE_KHR:
184 return "Surface";
185 case VK_OBJECT_TYPE_SWAPCHAIN_KHR:
186 return "Swapchain";
187 case VK_OBJECT_TYPE_DISPLAY_KHR:
188 return "Display";
189 case VK_OBJECT_TYPE_DISPLAY_MODE_KHR:
190 return "Display Mode";
191 case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT:
192 return "Debug Report Callback";
193 case VK_OBJECT_TYPE_OBJECT_TABLE_NVX:
194 return "Object Table";
195 case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX:
196 return "Indirect Commands Layout";
197 case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT:
198 return "Debug Utils Messenger";
199 case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT:
200 return "Validation Cache";
Shahbaz Youssefi38051222019-02-13 21:21:13 +0000201 case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NVX:
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500202 return "Acceleration Structure";
203 default:
204 return "<Unrecognized>";
205 }
206}
207
208VKAPI_ATTR VkBool32 VKAPI_CALL
209DebugUtilsMessenger(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
210 VkDebugUtilsMessageTypeFlagsEXT messageTypes,
211 const VkDebugUtilsMessengerCallbackDataEXT *callbackData,
212 void *userData)
213{
Shahbaz Youssefi0c01e362019-02-13 21:27:23 +0000214 constexpr VkDebugUtilsMessageSeverityFlagsEXT kSeveritiesToLog =
215 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
216 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
217
218 // Check if we even care about this message.
219 if ((messageSeverity & kSeveritiesToLog) == 0)
220 {
221 return VK_FALSE;
222 }
223
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500224 // See if it's an issue we are aware of and don't want to be spammed about.
225 if (IsIgnoredDebugMessage(callbackData->pMessageIdName))
226 {
227 return VK_FALSE;
228 }
229
230 std::ostringstream log;
Michael Spang25839802019-01-30 18:02:51 -0500231 if (callbackData->pMessageIdName)
232 {
233 log << "[ " << callbackData->pMessageIdName << " ] ";
234 }
235 log << callbackData->pMessage << std::endl;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500236
237 // Aesthetic value based on length of the function name, line number, etc.
238 constexpr size_t kStartIndent = 28;
239
240 // Output the debug marker hierarchy under which this error has occured.
241 size_t indent = kStartIndent;
242 if (callbackData->queueLabelCount > 0)
243 {
244 log << std::string(indent++, ' ') << "<Queue Label Hierarchy:>" << std::endl;
245 for (uint32_t i = 0; i < callbackData->queueLabelCount; ++i)
246 {
247 log << std::string(indent++, ' ') << callbackData->pQueueLabels[i].pLabelName
248 << std::endl;
249 }
250 }
251 if (callbackData->cmdBufLabelCount > 0)
252 {
253 log << std::string(indent++, ' ') << "<Command Buffer Label Hierarchy:>" << std::endl;
254 for (uint32_t i = 0; i < callbackData->cmdBufLabelCount; ++i)
255 {
256 log << std::string(indent++, ' ') << callbackData->pCmdBufLabels[i].pLabelName
257 << std::endl;
258 }
259 }
260 // Output the objects involved in this error message.
261 if (callbackData->objectCount > 0)
262 {
263 for (uint32_t i = 0; i < callbackData->objectCount; ++i)
264 {
265 const char *objectName = callbackData->pObjects[i].pObjectName;
266 const char *objectType = GetVkObjectTypeName(callbackData->pObjects[i].objectType);
267 uint64_t objectHandle = callbackData->pObjects[i].objectHandle;
268 log << std::string(indent, ' ') << "Object: ";
269 if (objectHandle == 0)
270 {
271 log << "VK_NULL_HANDLE";
272 }
273 else
274 {
275 log << "0x" << std::hex << objectHandle << std::dec;
276 }
277 log << " (type = " << objectType << "(" << callbackData->pObjects[i].objectType << "))";
278 if (objectName)
279 {
280 log << " [" << objectName << "]";
281 }
282 log << std::endl;
283 }
284 }
285
Jamie Madill54ed8f02019-02-11 12:32:04 -0500286 bool isError = (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0;
287 std::string msg = log.str();
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500288
289 if (isError)
290 {
Jamie Madill54ed8f02019-02-11 12:32:04 -0500291 ERR() << msg;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500292 }
293 else
294 {
Jamie Madill54ed8f02019-02-11 12:32:04 -0500295 WARN() << msg;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500296 }
297
298 return VK_FALSE;
299}
300
Yuly Novikov199f4292018-01-19 19:04:05 -0500301VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
302 VkDebugReportObjectTypeEXT objectType,
303 uint64_t object,
304 size_t location,
305 int32_t messageCode,
306 const char *layerPrefix,
307 const char *message,
308 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500309{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600310 if (IsIgnoredDebugMessage(message))
311 {
312 return VK_FALSE;
313 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500314 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
315 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500316 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500317#if !defined(NDEBUG)
318 // Abort the call in Debug builds.
319 return VK_TRUE;
320#endif
321 }
322 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
323 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500324 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500325 }
326 else
327 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500328 // Uncomment this if you want Vulkan spam.
329 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500330 }
331
332 return VK_FALSE;
333}
334
Yuly Novikov199f4292018-01-19 19:04:05 -0500335// If we're loading the validation layers, we could be running from any random directory.
336// Change to the executable directory so we can find the layers, then change back to the
337// previous directory to be safe we don't disrupt the application.
338class ScopedVkLoaderEnvironment : angle::NonCopyable
339{
340 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600341 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
342 : mEnableValidationLayers(enableValidationLayers),
343 mEnableMockICD(enableMockICD),
344 mChangedCWD(false),
345 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500346 {
347// Changing CWD and setting environment variables makes no sense on Android,
348// since this code is a part of Java application there.
349// Android Vulkan loader doesn't need this either.
Michael Spang229fc832019-01-21 18:09:15 -0500350#if !defined(ANGLE_PLATFORM_ANDROID) && !defined(ANGLE_PLATFORM_FUCHSIA)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600351 if (enableMockICD)
352 {
353 // Override environment variable to use built Mock ICD
354 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
355 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
356 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
357 if (!mChangedICDPath)
358 {
359 ERR() << "Error setting Path for Mock/Null Driver.";
360 mEnableMockICD = false;
361 }
362 }
Jamie Madill46848422018-08-09 10:46:06 -0400363 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500364 {
365 const auto &cwd = angle::GetCWD();
366 if (!cwd.valid())
367 {
368 ERR() << "Error getting CWD for Vulkan layers init.";
369 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400370 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500371 }
372 else
373 {
374 mPreviousCWD = cwd.value();
Jamie Madillbab03022019-01-16 14:12:28 -0500375 std::string exeDir = angle::GetExecutableDirectory();
376 mChangedCWD = angle::SetCWD(exeDir.c_str());
Yuly Novikov199f4292018-01-19 19:04:05 -0500377 if (!mChangedCWD)
378 {
379 ERR() << "Error setting CWD for Vulkan layers init.";
380 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400381 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500382 }
383 }
384 }
385
386 // Override environment variable to use the ANGLE layers.
387 if (mEnableValidationLayers)
388 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700389 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500390 {
391 ERR() << "Error setting environment for Vulkan layers init.";
392 mEnableValidationLayers = false;
393 }
394 }
395#endif // !defined(ANGLE_PLATFORM_ANDROID)
396 }
397
398 ~ScopedVkLoaderEnvironment()
399 {
400 if (mChangedCWD)
401 {
402#if !defined(ANGLE_PLATFORM_ANDROID)
403 ASSERT(mPreviousCWD.valid());
404 angle::SetCWD(mPreviousCWD.value().c_str());
405#endif // !defined(ANGLE_PLATFORM_ANDROID)
406 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600407 if (mChangedICDPath)
408 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600409 if (mPreviousICDPath.value().empty())
410 {
411 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
412 }
413 else
414 {
415 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
416 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600417 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500418 }
419
Jamie Madillaaca96e2018-06-12 10:19:48 -0400420 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500421
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600422 bool canEnableMockICD() const { return mEnableMockICD; }
423
Yuly Novikov199f4292018-01-19 19:04:05 -0500424 private:
425 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600426 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500427 bool mChangedCWD;
428 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600429 bool mChangedICDPath;
430 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500431};
432
Jamie Madill21061022018-07-12 23:56:30 -0400433void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
434 bool preferMockICD,
435 VkPhysicalDevice *physicalDeviceOut,
436 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
437{
438 ASSERT(!physicalDevices.empty());
439 if (preferMockICD)
440 {
441 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
442 {
443 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
444 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
445 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
446 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
447 {
448 *physicalDeviceOut = physicalDevice;
449 return;
450 }
451 }
452 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
453 "physicalDevice instead.";
454 }
455
456 // Fall back to first device.
457 *physicalDeviceOut = physicalDevices[0];
458 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
459}
Jamie Madill0da73fe2018-10-02 09:31:39 -0400460
461// Initially dumping the command graphs is disabled.
462constexpr bool kEnableCommandGraphDiagnostics = false;
Ian Elliottbcb78902018-12-19 11:46:29 -0700463
Jamie Madille09bd5d2016-11-29 16:20:35 -0500464} // anonymous namespace
465
Jamie Madill49ac74b2017-12-21 14:42:33 -0500466// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400467RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500468
Jamie Madillaaca96e2018-06-12 10:19:48 -0400469RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500470
471RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000472 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
Jamie Madillb980c562018-11-27 11:34:27 -0500473{}
Jamie Madill49ac74b2017-12-21 14:42:33 -0500474
475RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
476{
477 std::swap(commandPool, other.commandPool);
478 std::swap(fence, other.fence);
479 std::swap(serial, other.serial);
480 return *this;
481}
482
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000483void RendererVk::CommandBatch::destroy(VkDevice device)
Jamie Madillbea35a62018-07-05 11:54:10 -0400484{
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000485 commandPool.destroy(device);
Jamie Madillbea35a62018-07-05 11:54:10 -0400486 fence.destroy(device);
487}
488
Jamie Madill9f2a8612017-11-30 12:43:09 -0500489// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500490RendererVk::RendererVk()
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400491 : mDisplay(nullptr),
492 mCapsInitialized(false),
Ian Elliottbcb78902018-12-19 11:46:29 -0700493 mFeaturesInitialized(false),
Jamie Madill0448ec82016-12-23 13:41:47 -0500494 mInstance(VK_NULL_HANDLE),
495 mEnableValidationLayers(false),
Jamie Madill0ea96212018-10-30 15:14:51 -0400496 mEnableMockICD(false),
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500497 mDebugUtilsMessenger(VK_NULL_HANDLE),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500498 mDebugReportCallback(VK_NULL_HANDLE),
499 mPhysicalDevice(VK_NULL_HANDLE),
500 mQueue(VK_NULL_HANDLE),
501 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
Frank Henigman52047de2018-11-13 17:22:36 -0500502 mMaxVertexAttribDivisor(1),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500503 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400504 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400505 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400506 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400507 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400508 mCommandGraph(kEnableCommandGraphDiagnostics),
509 mGpuEventsEnabled(false),
510 mGpuClockSync{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()},
511 mGpuEventTimestampOrigin(0)
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500512{
513 VkFormatProperties invalid = {0, 0, kInvalidFormatFeatureFlags};
514 mFormatProperties.fill(invalid);
515}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400516
Jamie Madillb980c562018-11-27 11:34:27 -0500517RendererVk::~RendererVk() {}
Jamie Madill21061022018-07-12 23:56:30 -0400518
519void RendererVk::onDestroy(vk::Context *context)
520{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500521 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500522 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500523 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400524 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500525 }
526
Shahbaz Youssefie3219402018-12-08 16:54:14 +0100527 mUtils.destroy(mDevice);
Shahbaz Youssefi8f1b7a62018-11-14 16:02:54 -0500528
Jamie Madillc7918ce2018-06-13 13:25:31 -0400529 mPipelineLayoutCache.destroy(mDevice);
530 mDescriptorSetLayoutCache.destroy(mDevice);
531
Jamie Madill9f2a8612017-11-30 12:43:09 -0500532 mRenderPassCache.destroy(mDevice);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500533 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400534 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400535 mShaderLibrary.destroy(mDevice);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400536 mGpuEventQueryPool.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500537
Jamie Madill06ca6342018-07-12 15:56:53 -0400538 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500539
Jamie Madill5deea722017-02-16 10:44:46 -0500540 if (mCommandPool.valid())
541 {
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000542 mCommandPool.destroy(mDevice);
Jamie Madill5deea722017-02-16 10:44:46 -0500543 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500544
545 if (mDevice)
546 {
547 vkDestroyDevice(mDevice, nullptr);
548 mDevice = VK_NULL_HANDLE;
549 }
550
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500551 if (mDebugUtilsMessenger)
Jamie Madill0448ec82016-12-23 13:41:47 -0500552 {
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500553 ASSERT(mInstance && vkDestroyDebugUtilsMessengerEXT);
554 vkDestroyDebugUtilsMessengerEXT(mInstance, mDebugUtilsMessenger, nullptr);
555
556 ASSERT(mDebugReportCallback == VK_NULL_HANDLE);
557 }
558 else if (mDebugReportCallback)
559 {
560 ASSERT(mInstance && vkDestroyDebugReportCallbackEXT);
561 vkDestroyDebugReportCallbackEXT(mInstance, mDebugReportCallback, nullptr);
Jamie Madill0448ec82016-12-23 13:41:47 -0500562 }
563
Jamie Madill4d0bf552016-12-28 15:45:24 -0500564 if (mInstance)
565 {
566 vkDestroyInstance(mInstance, nullptr);
567 mInstance = VK_NULL_HANDLE;
568 }
569
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600570 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500571 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500572}
573
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400574void RendererVk::notifyDeviceLost()
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400575{
576 mDeviceLost = true;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400577
578 mCommandGraph.clear();
Jamie Madill85ca1892019-01-16 13:27:15 -0500579 nextSerial();
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400580 freeAllInFlightResources();
581
582 mDisplay->notifyDeviceLost();
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400583}
584
585bool RendererVk::isDeviceLost() const
586{
587 return mDeviceLost;
588}
589
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400590angle::Result RendererVk::initialize(DisplayVk *displayVk,
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400591 egl::Display *display,
Michael Spang740db7f2019-02-06 09:40:13 -0500592 const char *wsiExtension,
593 const char *wsiLayer)
Jamie Madill327ba852016-11-30 12:38:28 -0500594{
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400595 mDisplay = display;
596 const egl::AttributeMap &attribs = mDisplay->getAttributeMap();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600597 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
598 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500599 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madill0ea96212018-10-30 15:14:51 -0400600 mEnableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500601
Jamie Madill0448ec82016-12-23 13:41:47 -0500602 // Gather global layer properties.
603 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400604 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500605
606 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
607 if (instanceLayerCount > 0)
608 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400609 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
610 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500611 }
612
Michael Spang6c13c702019-02-06 15:59:44 -0500613 VulkanLayerVector enabledInstanceLayerNames;
614 if (mEnableValidationLayers)
615 {
616 bool layersRequested =
617 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
618 mEnableValidationLayers = GetAvailableValidationLayers(instanceLayerProps, layersRequested,
619 &enabledInstanceLayerNames);
620 }
621
622 if (wsiLayer)
623 {
624 enabledInstanceLayerNames.push_back(wsiLayer);
625 }
626
627 // Enumerate instance extensions that are provided by the vulkan
628 // implementation and implicit layers.
Jamie Madille09bd5d2016-11-29 16:20:35 -0500629 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400630 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400631 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500632
633 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
634 if (instanceExtensionCount > 0)
635 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400636 ANGLE_VK_TRY(displayVk,
637 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
638 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500639 }
640
Michael Spang6c13c702019-02-06 15:59:44 -0500641 // Enumerate instance extensions that are provided by explicit layers.
642 for (const char *layerName : enabledInstanceLayerNames)
Jamie Madill0448ec82016-12-23 13:41:47 -0500643 {
Michael Spang6c13c702019-02-06 15:59:44 -0500644 uint32_t previousExtensionCount = instanceExtensionProps.size();
645 uint32_t instanceLayerExtensionCount = 0;
646 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties(
647 layerName, &instanceLayerExtensionCount, nullptr));
648 instanceExtensionProps.resize(previousExtensionCount + instanceLayerExtensionCount);
649 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties(
650 layerName, &instanceLayerExtensionCount,
651 instanceExtensionProps.data() + previousExtensionCount));
Jamie Madill0448ec82016-12-23 13:41:47 -0500652 }
653
Frank Henigman52047de2018-11-13 17:22:36 -0500654 ExtensionNameList instanceExtensionNames;
655 if (!instanceExtensionProps.empty())
656 {
657 for (const VkExtensionProperties &i : instanceExtensionProps)
658 {
659 instanceExtensionNames.push_back(i.extensionName);
660 }
661 std::sort(instanceExtensionNames.begin(), instanceExtensionNames.end(), StrLess);
662 }
663
664 ExtensionNameList enabledInstanceExtensions;
Jamie Madille09bd5d2016-11-29 16:20:35 -0500665 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Michael Spang740db7f2019-02-06 09:40:13 -0500666 enabledInstanceExtensions.push_back(wsiExtension);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500667
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500668 bool enableDebugUtils =
669 mEnableValidationLayers &&
Frank Henigman52047de2018-11-13 17:22:36 -0500670 ExtensionFound(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instanceExtensionNames);
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500671 bool enableDebugReport =
672 mEnableValidationLayers && !enableDebugUtils &&
Frank Henigman52047de2018-11-13 17:22:36 -0500673 ExtensionFound(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instanceExtensionNames);
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500674
675 if (enableDebugUtils)
676 {
677 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
678 }
679 else if (enableDebugReport)
Jamie Madill0448ec82016-12-23 13:41:47 -0500680 {
681 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
682 }
683
Jamie Madille09bd5d2016-11-29 16:20:35 -0500684 // Verify the required extensions are in the extension names set. Fail if not.
Frank Henigman52047de2018-11-13 17:22:36 -0500685 std::sort(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(), StrLess);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400686 ANGLE_VK_TRY(displayVk,
Frank Henigman52047de2018-11-13 17:22:36 -0500687 VerifyExtensionsPresent(instanceExtensionNames, enabledInstanceExtensions));
688
689 // Enable VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME if available.
690 if (ExtensionFound(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
691 instanceExtensionNames))
692 {
693 enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
694 }
Jamie Madille09bd5d2016-11-29 16:20:35 -0500695
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400696 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500697 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500698 applicationInfo.pApplicationName = "ANGLE";
699 applicationInfo.applicationVersion = 1;
700 applicationInfo.pEngineName = "ANGLE";
701 applicationInfo.engineVersion = 1;
Ian Elliott899c5d22018-12-21 13:12:50 -0700702
703 auto enumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
704 vkGetInstanceProcAddr(mInstance, "vkEnumerateInstanceVersion"));
705 if (!enumerateInstanceVersion)
706 {
707 applicationInfo.apiVersion = VK_API_VERSION_1_0;
708 }
709 else
710 {
711 uint32_t apiVersion = VK_API_VERSION_1_0;
712 ANGLE_VK_TRY(displayVk, enumerateInstanceVersion(&apiVersion));
713 if ((VK_VERSION_MAJOR(apiVersion) > 1) || (VK_VERSION_MINOR(apiVersion) >= 1))
714 {
Ian Elliott356d26c2019-02-11 11:27:01 -0700715 // This is the highest version of core Vulkan functionality that ANGLE uses.
716 applicationInfo.apiVersion = kPreferredVulkanAPIVersion;
Ian Elliott899c5d22018-12-21 13:12:50 -0700717 }
718 else
719 {
Ian Elliott356d26c2019-02-11 11:27:01 -0700720 // Since only 1.0 instance-level functionality is available, this must set to 1.0.
Ian Elliott899c5d22018-12-21 13:12:50 -0700721 applicationInfo.apiVersion = VK_API_VERSION_1_0;
722 }
723 }
Jamie Madill327ba852016-11-30 12:38:28 -0500724
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400725 VkInstanceCreateInfo instanceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -0500726 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
727 instanceInfo.flags = 0;
728 instanceInfo.pApplicationInfo = &applicationInfo;
Jamie Madill327ba852016-11-30 12:38:28 -0500729
Jamie Madille09bd5d2016-11-29 16:20:35 -0500730 // Enable requested layers and extensions.
731 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
732 instanceInfo.ppEnabledExtensionNames =
733 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Michael Spang6c13c702019-02-06 15:59:44 -0500734 instanceInfo.enabledLayerCount = enabledInstanceLayerNames.size();
735 instanceInfo.ppEnabledLayerNames = enabledInstanceLayerNames.data();
Jamie Madill327ba852016-11-30 12:38:28 -0500736
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400737 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500738
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500739 if (enableDebugUtils)
Jamie Madill0448ec82016-12-23 13:41:47 -0500740 {
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500741 // Try to use the newer EXT_debug_utils if it exists.
742 InitDebugUtilsEXTFunctions(mInstance);
743
744 // Create the messenger callback.
745 VkDebugUtilsMessengerCreateInfoEXT messengerInfo = {};
746
747 messengerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
Shahbaz Youssefi0c01e362019-02-13 21:27:23 +0000748 messengerInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
749 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
750 messengerInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
751 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
752 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500753 messengerInfo.pfnUserCallback = &DebugUtilsMessenger;
754 messengerInfo.pUserData = this;
755
756 ANGLE_VK_TRY(displayVk, vkCreateDebugUtilsMessengerEXT(mInstance, &messengerInfo, nullptr,
757 &mDebugUtilsMessenger));
758 }
759 else if (enableDebugReport)
760 {
761 // Fallback to EXT_debug_report.
762 InitDebugReportEXTFunctions(mInstance);
763
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400764 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500765
766 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500767 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500768 debugReportInfo.pfnCallback = &DebugReportCallback;
769 debugReportInfo.pUserData = this;
770
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500771 ANGLE_VK_TRY(displayVk, vkCreateDebugReportCallbackEXT(mInstance, &debugReportInfo, nullptr,
772 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500773 }
774
Frank Henigman52047de2018-11-13 17:22:36 -0500775 if (std::find(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(),
776 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) !=
777 enabledInstanceExtensions.end())
778 {
779 InitGetPhysicalDeviceProperties2KHRFunctions(mInstance);
780 ASSERT(vkGetPhysicalDeviceProperties2KHR);
781 }
782
Jamie Madill4d0bf552016-12-28 15:45:24 -0500783 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400784 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
785 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500786
787 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700788 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400789 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
790 physicalDevices.data()));
Jamie Madill0ea96212018-10-30 15:14:51 -0400791 ChoosePhysicalDevice(physicalDevices, mEnableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700792 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500793
Jamie Madill30b5d842018-08-31 17:19:12 -0400794 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
795
Jamie Madill4d0bf552016-12-28 15:45:24 -0500796 // Ensure we can find a graphics queue family.
797 uint32_t queueCount = 0;
798 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
799
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400800 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500801
802 mQueueFamilyProperties.resize(queueCount);
803 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
804 mQueueFamilyProperties.data());
805
Jamie Madillb980c562018-11-27 11:34:27 -0500806 size_t graphicsQueueFamilyCount = false;
807 uint32_t firstGraphicsQueueFamily = 0;
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500808 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500809 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
810 {
811 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500812 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500813 {
814 ASSERT(queueInfo.queueCount > 0);
815 graphicsQueueFamilyCount++;
816 if (firstGraphicsQueueFamily == 0)
817 {
818 firstGraphicsQueueFamily = familyIndex;
819 }
820 break;
821 }
822 }
823
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400824 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500825
826 // If only one queue family, go ahead and initialize the device. If there is more than one
827 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
828 if (graphicsQueueFamilyCount == 1)
829 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400830 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500831 }
832
Jamie Madill035fd6b2017-10-03 15:43:22 -0400833 // Store the physical device memory properties so we can find the right memory pools.
834 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500835
Jamie Madill06ca6342018-07-12 15:56:53 -0400836 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500837
Jamie Madill6a89d222017-11-02 11:59:51 -0400838 // Initialize the format table.
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500839 mFormatTable.initialize(this, &mNativeTextureCaps, &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400840
Jamie Madill7c985f52018-11-29 18:16:17 -0500841 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400842}
843
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400844angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500845{
846 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400847 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400848 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500849
850 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
851 if (deviceLayerCount > 0)
852 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400853 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
854 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500855 }
856
Michael Spang6c13c702019-02-06 15:59:44 -0500857 VulkanLayerVector enabledDeviceLayerNames;
858 if (mEnableValidationLayers)
859 {
860 mEnableValidationLayers =
861 GetAvailableValidationLayers(deviceLayerProps, false, &enabledDeviceLayerNames);
862 }
863
864 const char *wsiLayer = displayVk->getWSILayer();
865 if (wsiLayer)
866 {
867 enabledDeviceLayerNames.push_back(wsiLayer);
868 }
869
870 // Enumerate device extensions that are provided by the vulkan
871 // implementation and implicit layers.
Jamie Madill4d0bf552016-12-28 15:45:24 -0500872 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400873 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
874 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500875
876 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
877 if (deviceExtensionCount > 0)
878 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400879 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
880 &deviceExtensionCount,
881 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500882 }
883
Michael Spang6c13c702019-02-06 15:59:44 -0500884 // Enumerate device extensions that are provided by explicit layers.
885 for (const char *layerName : enabledDeviceLayerNames)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500886 {
Michael Spang6c13c702019-02-06 15:59:44 -0500887 uint32_t previousExtensionCount = deviceExtensionProps.size();
888 uint32_t deviceLayerExtensionCount = 0;
889 ANGLE_VK_TRY(displayVk,
890 vkEnumerateDeviceExtensionProperties(mPhysicalDevice, layerName,
891 &deviceLayerExtensionCount, nullptr));
892 deviceExtensionProps.resize(previousExtensionCount + deviceLayerExtensionCount);
893 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(
894 mPhysicalDevice, layerName, &deviceLayerExtensionCount,
895 deviceExtensionProps.data() + previousExtensionCount));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500896 }
897
Frank Henigman52047de2018-11-13 17:22:36 -0500898 ExtensionNameList deviceExtensionNames;
899 if (!deviceExtensionProps.empty())
900 {
901 ASSERT(deviceExtensionNames.size() <= deviceExtensionProps.size());
902 for (const VkExtensionProperties &prop : deviceExtensionProps)
903 {
904 deviceExtensionNames.push_back(prop.extensionName);
905 }
906 std::sort(deviceExtensionNames.begin(), deviceExtensionNames.end(), StrLess);
907 }
908
909 ExtensionNameList enabledDeviceExtensions;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500910 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
911
Frank Henigman52047de2018-11-13 17:22:36 -0500912 initFeatures(deviceExtensionNames);
Ian Elliottbcb78902018-12-19 11:46:29 -0700913 mFeaturesInitialized = true;
914
Luc Ferronbf6dc372018-06-28 15:24:19 -0400915 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
Ian Elliott52f5da42018-12-21 09:02:09 -0700916 if ((getFeatures().flipViewportY) &&
917 (mPhysicalDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0)))
Luc Ferronbf6dc372018-06-28 15:24:19 -0400918 {
919 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
920 }
Ian Elliottbcb78902018-12-19 11:46:29 -0700921 if (getFeatures().supportsIncrementalPresent)
922 {
923 enabledDeviceExtensions.push_back(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME);
924 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400925
Frank Henigman52047de2018-11-13 17:22:36 -0500926 std::sort(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(), StrLess);
927 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionNames, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500928
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400929 // Select additional features to be enabled
Frank Henigman52047de2018-11-13 17:22:36 -0500930 VkPhysicalDeviceFeatures2KHR enabledFeatures = {};
931 enabledFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
932 enabledFeatures.features.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
933 enabledFeatures.features.robustBufferAccess = mPhysicalDeviceFeatures.robustBufferAccess;
Shahbaz Youssefi962c2222019-02-20 15:43:41 -0500934 enabledFeatures.features.samplerAnisotropy = mPhysicalDeviceFeatures.samplerAnisotropy;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400935
Frank Henigman52047de2018-11-13 17:22:36 -0500936 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT divisorFeatures = {};
937 divisorFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT;
938 divisorFeatures.vertexAttributeInstanceRateDivisor = true;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500939
940 float zeroPriority = 0.0f;
Frank Henigman52047de2018-11-13 17:22:36 -0500941 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500942 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500943 queueCreateInfo.flags = 0;
944 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
945 queueCreateInfo.queueCount = 1;
946 queueCreateInfo.pQueuePriorities = &zeroPriority;
947
948 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400949 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500950
Jamie Madill50cf2be2018-06-15 09:46:57 -0400951 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400952 createInfo.flags = 0;
953 createInfo.queueCreateInfoCount = 1;
954 createInfo.pQueueCreateInfos = &queueCreateInfo;
Michael Spang6c13c702019-02-06 15:59:44 -0500955 createInfo.enabledLayerCount = enabledDeviceLayerNames.size();
956 createInfo.ppEnabledLayerNames = enabledDeviceLayerNames.data();
Frank Henigman52047de2018-11-13 17:22:36 -0500957
958 if (vkGetPhysicalDeviceProperties2KHR &&
959 ExtensionFound(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, deviceExtensionNames))
960 {
961 enabledDeviceExtensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME);
962 enabledFeatures.pNext = &divisorFeatures;
963
964 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT divisorProperties = {};
965 divisorProperties.sType =
966 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT;
967
968 VkPhysicalDeviceProperties2 deviceProperties = {};
969 deviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
970 deviceProperties.pNext = &divisorProperties;
971
972 vkGetPhysicalDeviceProperties2KHR(mPhysicalDevice, &deviceProperties);
973 mMaxVertexAttribDivisor = divisorProperties.maxVertexAttribDivisor;
974
975 createInfo.pNext = &enabledFeatures;
976 }
977 else
978 {
979 createInfo.pEnabledFeatures = &enabledFeatures.features;
980 }
981
Jamie Madill4d0bf552016-12-28 15:45:24 -0500982 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
983 createInfo.ppEnabledExtensionNames =
984 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500985
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400986 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500987
988 mCurrentQueueFamilyIndex = queueFamilyIndex;
989
990 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
991
992 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400993 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -0500994 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
995 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
996 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500997
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000998 ANGLE_VK_TRY(displayVk, mCommandPool.init(mDevice, commandPoolInfo));
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400999
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001000 // Initialize the vulkan pipeline cache.
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001001 ANGLE_TRY(initPipelineCache(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001002
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001003 // Initialize the submission semaphore pool.
1004 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
1005
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001006#if ANGLE_ENABLE_VULKAN_GPU_TRACE_EVENTS
1007 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1008 ASSERT(platform);
1009
1010 // GPU tracing workaround for anglebug.com/2927. The renderer should not emit gpu events during
1011 // platform discovery.
1012 const unsigned char *gpuEventsEnabled =
1013 platform->getTraceCategoryEnabledFlag(platform, "gpu.angle.gpu");
1014 mGpuEventsEnabled = gpuEventsEnabled && *gpuEventsEnabled;
1015#endif
1016
1017 if (mGpuEventsEnabled)
1018 {
1019 // Calculate the difference between CPU and GPU clocks for GPU event reporting.
1020 ANGLE_TRY(mGpuEventQueryPool.init(displayVk, VK_QUERY_TYPE_TIMESTAMP,
1021 vk::kDefaultTimestampQueryPoolSize));
1022 ANGLE_TRY(synchronizeCpuGpuTime(displayVk));
1023 }
1024
Jamie Madill7c985f52018-11-29 18:16:17 -05001025 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001026}
1027
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001028angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -04001029 VkSurfaceKHR surface,
1030 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001031{
1032 // We've already initialized a device, and can't re-create it unless it's never been used.
1033 // TODO(jmadill): Handle the re-creation case if necessary.
1034 if (mDevice != VK_NULL_HANDLE)
1035 {
1036 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
1037
1038 // Check if the current device supports present on this surface.
1039 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001040 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -04001041 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -05001042 surface, &supportsPresent));
1043
Jamie Madill6cad7732018-07-11 09:01:17 -04001044 if (supportsPresent == VK_TRUE)
1045 {
1046 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill7c985f52018-11-29 18:16:17 -05001047 return angle::Result::Continue;
Jamie Madill6cad7732018-07-11 09:01:17 -04001048 }
Jamie Madill4d0bf552016-12-28 15:45:24 -05001049 }
1050
1051 // Find a graphics and present queue.
1052 Optional<uint32_t> newPresentQueue;
1053 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
Shahbaz Youssefi823d8972018-11-13 10:52:40 -05001054 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001055 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
1056 {
1057 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -05001058 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001059 {
1060 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001061 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
1062 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001063
1064 if (supportsPresent == VK_TRUE)
1065 {
1066 newPresentQueue = queueIndex;
1067 break;
1068 }
1069 }
1070 }
1071
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001072 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
1073 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001074
Jamie Madill6cad7732018-07-11 09:01:17 -04001075 *presentQueueOut = newPresentQueue.value();
Jamie Madill7c985f52018-11-29 18:16:17 -05001076 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001077}
1078
1079std::string RendererVk::getVendorString() const
1080{
Olli Etuahoc6a06182018-04-13 14:11:46 +03001081 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -05001082}
1083
Jamie Madille09bd5d2016-11-29 16:20:35 -05001084std::string RendererVk::getRendererDescription() const
1085{
Jamie Madill4d0bf552016-12-28 15:45:24 -05001086 std::stringstream strstr;
1087
1088 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
1089
1090 strstr << "Vulkan ";
1091 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
1092 strstr << VK_VERSION_MINOR(apiVersion) << ".";
1093 strstr << VK_VERSION_PATCH(apiVersion);
1094
Olli Etuahoc6a06182018-04-13 14:11:46 +03001095 strstr << "(";
1096
1097 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
1098 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
1099 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
1100 // driver detection.
1101 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
1102 {
1103 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
1104 }
1105
Geoff Langa7af56b2018-12-14 14:20:28 -05001106 strstr << mPhysicalDeviceProperties.deviceName;
1107 strstr << " (" << gl::FmtHex(mPhysicalDeviceProperties.deviceID) << ")";
1108
1109 strstr << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -05001110
1111 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -05001112}
1113
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001114gl::Version RendererVk::getMaxSupportedESVersion() const
1115{
Geoff Lang0c2c9232019-01-14 16:01:38 -05001116 // Current highest supported version
1117 // TODO: Update this to support ES 3.0. http://crbug.com/angleproject/2950
1118 gl::Version maxVersion = gl::Version(2, 0);
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001119
Geoff Lang0c2c9232019-01-14 16:01:38 -05001120 // Vulkan inherited queries are required to support any GL query type
1121 if (!mPhysicalDeviceFeatures.inheritedQueries)
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001122 {
Geoff Lang0c2c9232019-01-14 16:01:38 -05001123 maxVersion = std::max(maxVersion, gl::Version(2, 0));
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001124 }
1125
Geoff Lang0c2c9232019-01-14 16:01:38 -05001126 return maxVersion;
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001127}
1128
Frank Henigman52047de2018-11-13 17:22:36 -05001129void RendererVk::initFeatures(const ExtensionNameList &deviceExtensionNames)
Jamie Madill12222072018-07-11 14:59:48 -04001130{
Jamie Madillb36a4812018-09-25 10:15:11 -04001131// Use OpenGL line rasterization rules by default.
1132// TODO(jmadill): Fix Android support. http://anglebug.com/2830
1133#if defined(ANGLE_PLATFORM_ANDROID)
1134 mFeatures.basicGLLineRasterization = false;
1135#else
Jamie Madill12222072018-07-11 14:59:48 -04001136 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -04001137#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -04001138
Ian Elliott52f5da42018-12-21 09:02:09 -07001139 if ((mPhysicalDeviceProperties.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) ||
Frank Henigman52047de2018-11-13 17:22:36 -05001140 ExtensionFound(VK_KHR_MAINTENANCE1_EXTENSION_NAME, deviceExtensionNames))
Ian Elliottd50521f2018-12-20 12:05:14 -07001141 {
1142 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
1143 // investigation. http://anglebug.com/2728
1144 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
1145 }
Frank Henigmanbeb669d2018-09-21 16:25:52 -04001146
1147#ifdef ANGLE_PLATFORM_WINDOWS
1148 // http://anglebug.com/2838
1149 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
Shahbaz Youssefi4f3b2072019-01-01 14:48:25 -05001150
1151 // http://anglebug.com/3055
1152 mFeatures.forceCpuPathForCubeMapCopy = IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -04001153#endif
Shahbaz Youssefid856ca42018-10-31 16:55:12 -04001154
1155 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1156 platform->overrideFeaturesVk(platform, &mFeatures);
Jamie Madillfde74c02018-11-18 16:12:02 -05001157
1158 // Work around incorrect NVIDIA point size range clamping.
1159 // TODO(jmadill): Narrow driver range once fixed. http://anglebug.com/2970
1160 if (IsNvidia(mPhysicalDeviceProperties.vendorID))
1161 {
1162 mFeatures.clampPointSize = true;
1163 }
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001164
Jamie Madillfa7ca182019-01-15 11:20:58 -05001165 // We also need to clamp point size on several Android drivers.
1166 // TODO(jmadill): Remove suppression once fixed. http://anglebug.com/2599
1167 if (IsAndroid())
1168 {
1169 mFeatures.clampPointSize = true;
1170 }
1171
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001172#if defined(ANGLE_PLATFORM_ANDROID)
Shahbaz Youssefib08457d2018-12-11 15:13:54 -05001173 // Work around ineffective compute-graphics barriers on Nexus 5X.
1174 // TODO(syoussefi): Figure out which other vendors and driver versions are affected.
1175 // http://anglebug.com/3019
1176 mFeatures.flushAfterVertexConversion =
1177 IsNexus5X(mPhysicalDeviceProperties.vendorID, mPhysicalDeviceProperties.deviceID);
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001178#endif
Ian Elliottbcb78902018-12-19 11:46:29 -07001179
Frank Henigman52047de2018-11-13 17:22:36 -05001180 if (ExtensionFound(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, deviceExtensionNames))
Ian Elliottbcb78902018-12-19 11:46:29 -07001181 {
1182 mFeatures.supportsIncrementalPresent = true;
1183 }
Jamie Madill12222072018-07-11 14:59:48 -04001184}
1185
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001186void RendererVk::initPipelineCacheVkKey()
1187{
1188 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
1189 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
1190 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
1191 // there is no '\0' in the result.
1192 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
1193 {
1194 hashStream << std::hex << c;
1195 }
1196 // Add the vendor and device id too for good measure.
1197 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
1198 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
1199
1200 const std::string &hashString = hashStream.str();
1201 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
1202 hashString.length(), mPipelineCacheVkBlobKey.data());
1203}
1204
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001205angle::Result RendererVk::initPipelineCache(DisplayVk *display)
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001206{
1207 initPipelineCacheVkKey();
1208
1209 egl::BlobCache::Value initialData;
1210 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
1211 mPipelineCacheVkBlobKey, &initialData);
1212
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001213 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001214
1215 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001216 pipelineCacheCreateInfo.flags = 0;
1217 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
1218 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
1219
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001220 ANGLE_VK_TRY(display, mPipelineCache.init(mDevice, pipelineCacheCreateInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -05001221 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001222}
1223
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001224void RendererVk::getSubmitWaitSemaphores(
1225 vk::Context *context,
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001226 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores,
1227 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> *waitStageMasks)
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001228{
1229 if (mSubmitLastSignaledSemaphore.getSemaphore())
1230 {
1231 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001232 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001233
1234 // Return the semaphore to the pool (which will remain valid and unused until the
1235 // queue it's about to be waited on has finished execution).
1236 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1237 }
1238
1239 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
1240 {
1241 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001242 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
1243
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001244 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
1245 }
1246 mSubmitWaitSemaphores.clear();
1247}
1248
Jamie Madillacccc6c2016-05-03 17:22:10 -04001249const gl::Caps &RendererVk::getNativeCaps() const
1250{
1251 ensureCapsInitialized();
1252 return mNativeCaps;
1253}
1254
1255const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
1256{
1257 ensureCapsInitialized();
1258 return mNativeTextureCaps;
1259}
1260
1261const gl::Extensions &RendererVk::getNativeExtensions() const
1262{
1263 ensureCapsInitialized();
1264 return mNativeExtensions;
1265}
1266
1267const gl::Limitations &RendererVk::getNativeLimitations() const
1268{
1269 ensureCapsInitialized();
1270 return mNativeLimitations;
1271}
1272
Luc Ferrondaedf4d2018-03-16 09:28:53 -04001273uint32_t RendererVk::getMaxActiveTextures()
1274{
1275 // TODO(lucferron): expose this limitation to GL in Context Caps
1276 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
1277 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
1278}
1279
Jamie Madill49ac74b2017-12-21 14:42:33 -05001280const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -05001281{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001282 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001283}
1284
Jamie Madill21061022018-07-12 23:56:30 -04001285angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001286{
Jamie Madill1f46bc12018-02-20 16:09:43 -05001287 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -05001288 {
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001289 TRACE_EVENT0("gpu.angle", "RendererVk::finish");
1290
Luc Ferron1617e692018-07-11 11:08:19 -04001291 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1292 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001293
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001294 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001295 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1296 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001297
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001298 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001299 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001300 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1301 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001302 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001303 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -04001304 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001305 submitInfo.signalSemaphoreCount = 0;
1306 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001307
Jamie Madill21061022018-07-12 23:56:30 -04001308 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001309 }
Jamie Madill4d0bf552016-12-28 15:45:24 -05001310
Jamie Madill4c26fc22017-02-24 11:04:10 -05001311 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -04001312 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001313 freeAllInFlightResources();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001314
1315 if (mGpuEventsEnabled)
1316 {
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001317 // This loop should in practice execute once since the queue is already idle.
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001318 while (mInFlightGpuEventQueries.size() > 0)
1319 {
1320 ANGLE_TRY(checkCompletedGpuEvents(context));
1321 }
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001322 // Recalculate the CPU/GPU time difference to account for clock drifting. Avoid unnecessary
1323 // synchronization if there is no event to be adjusted (happens when finish() gets called
1324 // multiple times towards the end of the application).
1325 if (mGpuEvents.size() > 0)
1326 {
1327 ANGLE_TRY(synchronizeCpuGpuTime(context));
1328 }
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001329 }
1330
Jamie Madill7c985f52018-11-29 18:16:17 -05001331 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001332}
1333
Jamie Madill0c0dc342017-03-24 14:18:51 -04001334void RendererVk::freeAllInFlightResources()
1335{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001336 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -04001337 {
Yuly Novikovb56ddbb2018-11-02 16:53:18 -04001338 // On device loss we need to wait for fence to be signaled before destroying it
1339 if (mDeviceLost)
1340 {
1341 VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs);
1342 // If wait times out, it is probably not possible to recover from lost device
1343 ASSERT(status == VK_SUCCESS || status == VK_ERROR_DEVICE_LOST);
1344 }
Jamie Madill49ac74b2017-12-21 14:42:33 -05001345 batch.fence.destroy(mDevice);
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001346 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001347 }
1348 mInFlightCommands.clear();
1349
1350 for (auto &garbage : mGarbage)
1351 {
Jamie Madille88ec8e2017-10-31 17:18:14 -04001352 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001353 }
1354 mGarbage.clear();
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001355
1356 mLastCompletedQueueSerial = mLastSubmittedQueueSerial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001357}
1358
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001359angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001360{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001361 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -05001362
Jamie Madill49ac74b2017-12-21 14:42:33 -05001363 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001364 {
Yuly Novikov27780292018-11-09 11:19:49 -05001365 VkResult result = batch.fence.getStatus(mDevice);
1366 if (result == VK_NOT_READY)
1367 {
Jamie Madill0c0dc342017-03-24 14:18:51 -04001368 break;
Yuly Novikov27780292018-11-09 11:19:49 -05001369 }
1370 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001371
Jamie Madill49ac74b2017-12-21 14:42:33 -05001372 ASSERT(batch.serial > mLastCompletedQueueSerial);
1373 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001374
Jamie Madill49ac74b2017-12-21 14:42:33 -05001375 batch.fence.destroy(mDevice);
Tobin Ehlis4a419142019-02-05 08:50:30 -07001376 TRACE_EVENT0("gpu.angle", "commandPool.destroy");
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001377 batch.commandPool.destroy(mDevice);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001378 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001379 }
1380
Jamie Madill49ac74b2017-12-21 14:42:33 -05001381 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001382
1383 size_t freeIndex = 0;
1384 for (; freeIndex < mGarbage.size(); ++freeIndex)
1385 {
Jamie Madill49ac74b2017-12-21 14:42:33 -05001386 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -04001387 break;
1388 }
1389
1390 // Remove the entries from the garbage list - they should be ready to go.
1391 if (freeIndex > 0)
1392 {
1393 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -05001394 }
1395
Jamie Madill7c985f52018-11-29 18:16:17 -05001396 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001397}
1398
Jamie Madill21061022018-07-12 23:56:30 -04001399angle::Result RendererVk::submitFrame(vk::Context *context,
1400 const VkSubmitInfo &submitInfo,
1401 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001402{
Tobin Ehlis573f76b2018-05-03 11:10:44 -06001403 TRACE_EVENT0("gpu.angle", "RendererVk::submitFrame");
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001404 VkFenceCreateInfo fenceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -05001405 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1406 fenceInfo.flags = 0;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001407
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001408 vk::Scoped<CommandBatch> scopedBatch(mDevice);
Jamie Madillbea35a62018-07-05 11:54:10 -04001409 CommandBatch &batch = scopedBatch.get();
Yuly Novikov27780292018-11-09 11:19:49 -05001410 ANGLE_VK_TRY(context, batch.fence.init(mDevice, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001411
Jamie Madill21061022018-07-12 23:56:30 -04001412 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -05001413
1414 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -05001415 batch.commandPool = std::move(mCommandPool);
1416 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001417
Jamie Madillbea35a62018-07-05 11:54:10 -04001418 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -04001419
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001420 // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on
1421 // swap() though, and there could be multiple submissions in between (through glFlush() calls),
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001422 // so the limit is larger than the expected number of images. The
1423 // InterleavedAttributeDataBenchmark perf test for example issues a large number of flushes.
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001424 ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001425
Jamie Madill85ca1892019-01-16 13:27:15 -05001426 nextSerial();
Jamie Madill0c0dc342017-03-24 14:18:51 -04001427
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001428 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001429
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001430 if (mGpuEventsEnabled)
1431 {
1432 ANGLE_TRY(checkCompletedGpuEvents(context));
1433 }
1434
Jamie Madill49ac74b2017-12-21 14:42:33 -05001435 // Simply null out the command buffer here - it was allocated using the command pool.
1436 commandBuffer.releaseHandle();
1437
1438 // Reallocate the command pool for next frame.
1439 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001440 VkCommandPoolCreateInfo poolInfo = {};
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001441 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001442 poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001443 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001444
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001445 ANGLE_VK_TRY(context, mCommandPool.init(mDevice, poolInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -05001446 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001447}
1448
Jamie Madill85ca1892019-01-16 13:27:15 -05001449void RendererVk::nextSerial()
1450{
1451 // Increment the queue serial. If this fails, we should restart ANGLE.
1452 mLastSubmittedQueueSerial = mCurrentQueueSerial;
1453 mCurrentQueueSerial = mQueueSerialFactory.generate();
1454
1455 // Notify the Contexts that they should be starting new command buffers.
1456 // We use one command pool per serial/submit associated with this VkQueue. We can also
1457 // have multiple Contexts sharing one VkQueue. In ContextVk::setupDraw we don't explicitly
1458 // check for a new serial when starting a new command buffer. We just check that the current
1459 // recording command buffer is valid. Thus we need to explicitly notify every other Context
1460 // using this VkQueue that they their current command buffer is no longer valid.
1461 for (gl::Context *context : mDisplay->getContextSet())
1462 {
1463 ContextVk *contextVk = vk::GetImpl(context);
1464 contextVk->onCommandBufferFinished();
1465 }
1466}
1467
Jamie Madillaaca96e2018-06-12 10:19:48 -04001468bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -05001469{
1470 return serial > mLastCompletedQueueSerial;
1471}
1472
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001473angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
1474{
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001475 bool timedOut = false;
1476 angle::Result result = finishToSerialOrTimeout(context, serial, kMaxFenceWaitTimeNs, &timedOut);
1477
1478 // Don't tolerate timeout. If such a large wait time results in timeout, something's wrong.
1479 if (timedOut)
1480 {
1481 result = angle::Result::Stop;
1482 }
1483 return result;
1484}
1485
1486angle::Result RendererVk::finishToSerialOrTimeout(vk::Context *context,
1487 Serial serial,
1488 uint64_t timeout,
1489 bool *outTimedOut)
1490{
1491 *outTimedOut = false;
1492
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001493 if (!isSerialInUse(serial) || mInFlightCommands.empty())
1494 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001495 return angle::Result::Continue;
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001496 }
1497
1498 // Find the first batch with serial equal to or bigger than given serial (note that
1499 // the batch serials are unique, otherwise upper-bound would have been necessary).
1500 size_t batchIndex = mInFlightCommands.size() - 1;
1501 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
1502 {
1503 if (mInFlightCommands[i].serial >= serial)
1504 {
1505 batchIndex = i;
1506 break;
1507 }
1508 }
1509 const CommandBatch &batch = mInFlightCommands[batchIndex];
1510
1511 // Wait for it finish
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001512 VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs);
1513
1514 // If timed out, report it as such.
1515 if (status == VK_TIMEOUT)
1516 {
1517 *outTimedOut = true;
1518 return angle::Result::Continue;
1519 }
1520
1521 ANGLE_VK_TRY(context, status);
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001522
1523 // Clean up finished batches.
1524 return checkCompletedCommands(context);
1525}
1526
Jamie Madill21061022018-07-12 23:56:30 -04001527angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1528 const vk::RenderPassDesc &desc,
1529 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001530{
Jamie Madill21061022018-07-12 23:56:30 -04001531 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001532 renderPassOut);
1533}
1534
Jamie Madill21061022018-07-12 23:56:30 -04001535angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1536 const vk::RenderPassDesc &desc,
1537 const vk::AttachmentOpsArray &ops,
1538 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001539{
Jamie Madill21061022018-07-12 23:56:30 -04001540 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001541 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001542}
1543
Jamie Madilla5e06072018-05-18 14:36:05 -04001544vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001545{
Jamie Madilla5e06072018-05-18 14:36:05 -04001546 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001547}
1548
Jamie Madill21061022018-07-12 23:56:30 -04001549angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001550{
Jamie Madill21061022018-07-12 23:56:30 -04001551 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001552 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001553}
1554
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001555angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001556{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001557 if (mCommandGraph.empty())
1558 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001559 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001560 }
1561
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001562 TRACE_EVENT0("gpu.angle", "RendererVk::flush");
1563
Jamie Madillbea35a62018-07-05 11:54:10 -04001564 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1565 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001566
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001567 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001568 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1569 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001570
1571 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1572 // will be waited on.
1573 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001574
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001575 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001576 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001577 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1578 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001579 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001580 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001581 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001582 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001583 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001584
Jamie Madill21061022018-07-12 23:56:30 -04001585 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001586
Jamie Madill7c985f52018-11-29 18:16:17 -05001587 return angle::Result::Continue;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001588}
1589
Jamie Madill78feddc2018-04-27 11:45:05 -04001590Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001591{
Jamie Madill78feddc2018-04-27 11:45:05 -04001592 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001593}
1594
Jamie Madill21061022018-07-12 23:56:30 -04001595angle::Result RendererVk::getDescriptorSetLayout(
1596 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001597 const vk::DescriptorSetLayoutDesc &desc,
1598 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1599{
Jamie Madill21061022018-07-12 23:56:30 -04001600 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001601}
1602
Jamie Madill21061022018-07-12 23:56:30 -04001603angle::Result RendererVk::getPipelineLayout(
1604 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001605 const vk::PipelineLayoutDesc &desc,
1606 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1607 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1608{
Jamie Madill21061022018-07-12 23:56:30 -04001609 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001610 pipelineLayoutOut);
1611}
1612
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001613angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1614{
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001615 ASSERT(mPipelineCache.valid());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001616
1617 if (--mPipelineCacheVkUpdateTimeout > 0)
1618 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001619 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001620 }
1621
1622 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1623
1624 // Get the size of the cache.
1625 size_t pipelineCacheSize = 0;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001626 VkResult result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, nullptr);
Yuly Novikov27780292018-11-09 11:19:49 -05001627 if (result != VK_INCOMPLETE)
1628 {
1629 ANGLE_VK_TRY(displayVk, result);
1630 }
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001631
1632 angle::MemoryBuffer *pipelineCacheData = nullptr;
1633 ANGLE_VK_CHECK_ALLOC(displayVk,
1634 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1635
1636 size_t originalPipelineCacheSize = pipelineCacheSize;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001637 result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, pipelineCacheData->data());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001638 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1639 // was determined just above), so receiving it hints at an implementation bug we would want
1640 // to know about early.
Yuly Novikov27780292018-11-09 11:19:49 -05001641 ASSERT(result != VK_INCOMPLETE);
1642 ANGLE_VK_TRY(displayVk, result);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001643
1644 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1645 // the buffer to avoid leaking garbage memory.
1646 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1647 if (pipelineCacheSize < originalPipelineCacheSize)
1648 {
1649 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1650 originalPipelineCacheSize - pipelineCacheSize);
1651 }
1652
1653 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1654
Jamie Madill7c985f52018-11-29 18:16:17 -05001655 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001656}
1657
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001658angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1659 const vk::Semaphore **outSemaphore)
1660{
1661 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1662
1663 vk::SemaphoreHelper semaphore;
1664 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1665
1666 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1667 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1668
Jamie Madill7c985f52018-11-29 18:16:17 -05001669 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001670}
1671
1672const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1673{
1674 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1675
1676 // Return the semaphore to the pool (which will remain valid and unused until the
1677 // queue it's about to be waited on has finished execution). The caller is about
1678 // to wait on it.
1679 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1680
1681 return semaphore;
1682}
1683
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001684angle::Result RendererVk::getTimestamp(vk::Context *context, uint64_t *timestampOut)
1685{
1686 // The intent of this function is to query the timestamp without stalling the GPU. Currently,
1687 // that seems impossible, so instead, we are going to make a small submission with just a
1688 // timestamp query. First, the disjoint timer query extension says:
1689 //
1690 // > This will return the GL time after all previous commands have reached the GL server but
1691 // have not yet necessarily executed.
1692 //
1693 // The previous commands are stored in the command graph at the moment and are not yet flushed.
1694 // The wording allows us to make a submission to get the timestamp without performing a flush.
1695 //
1696 // Second:
1697 //
1698 // > By using a combination of this synchronous get command and the asynchronous timestamp query
1699 // object target, applications can measure the latency between when commands reach the GL server
1700 // and when they are realized in the framebuffer.
1701 //
1702 // This fits with the above strategy as well, although inevitably we are possibly introducing a
1703 // GPU bubble. This function directly generates a command buffer and submits it instead of
1704 // using the other member functions. This is to avoid changing any state, such as the queue
1705 // serial.
1706
1707 // Create a query used to receive the GPU timestamp
1708 vk::Scoped<vk::DynamicQueryPool> timestampQueryPool(mDevice);
1709 vk::QueryHelper timestampQuery;
1710 ANGLE_TRY(timestampQueryPool.get().init(context, VK_QUERY_TYPE_TIMESTAMP, 1));
1711 ANGLE_TRY(timestampQueryPool.get().allocateQuery(context, &timestampQuery));
1712
1713 // Record the command buffer
1714 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1715 vk::CommandBuffer &commandBuffer = commandBatch.get();
1716
1717 VkCommandBufferAllocateInfo commandBufferInfo = {};
1718 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1719 commandBufferInfo.commandPool = mCommandPool.getHandle();
1720 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1721 commandBufferInfo.commandBufferCount = 1;
1722
Yuly Novikov27780292018-11-09 11:19:49 -05001723 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001724
1725 VkCommandBufferBeginInfo beginInfo = {};
1726 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1727 beginInfo.flags = 0;
1728 beginInfo.pInheritanceInfo = nullptr;
1729
Yuly Novikov27780292018-11-09 11:19:49 -05001730 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001731
1732 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1733 timestampQuery.getQuery(), 1);
1734 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1735 timestampQuery.getQueryPool()->getHandle(),
1736 timestampQuery.getQuery());
1737
Yuly Novikov27780292018-11-09 11:19:49 -05001738 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001739
1740 // Create fence for the submission
1741 VkFenceCreateInfo fenceInfo = {};
1742 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1743 fenceInfo.flags = 0;
1744
1745 vk::Scoped<vk::Fence> fence(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001746 ANGLE_VK_TRY(context, fence.get().init(mDevice, fenceInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001747
1748 // Submit the command buffer
1749 VkSubmitInfo submitInfo = {};
1750 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1751 submitInfo.waitSemaphoreCount = 0;
1752 submitInfo.pWaitSemaphores = nullptr;
1753 submitInfo.pWaitDstStageMask = nullptr;
1754 submitInfo.commandBufferCount = 1;
1755 submitInfo.pCommandBuffers = commandBuffer.ptr();
1756 submitInfo.signalSemaphoreCount = 0;
1757 submitInfo.pSignalSemaphores = nullptr;
1758
1759 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, fence.get().getHandle()));
1760
1761 // Wait for the submission to finish. Given no semaphores, there is hope that it would execute
1762 // in parallel with what's already running on the GPU.
Yuly Novikov27780292018-11-09 11:19:49 -05001763 ANGLE_VK_TRY(context, fence.get().wait(mDevice, kMaxFenceWaitTimeNs));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001764
1765 // Get the query results
1766 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1767
Yuly Novikov27780292018-11-09 11:19:49 -05001768 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
1769 mDevice, timestampQuery.getQuery(), 1, sizeof(*timestampOut),
1770 timestampOut, sizeof(*timestampOut), queryFlags));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001771
1772 timestampQueryPool.get().freeQuery(context, &timestampQuery);
1773
Shahbaz Youssefi5904ee32019-01-25 11:15:16 -05001774 // Convert results to nanoseconds.
1775 *timestampOut = static_cast<uint64_t>(
1776 *timestampOut * static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod));
1777
Jamie Madill7c985f52018-11-29 18:16:17 -05001778 return angle::Result::Continue;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001779}
1780
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05001781// These functions look at the mandatory format for support, and fallback to querying the device (if
1782// necessary) to test the availability of the bits.
1783bool RendererVk::hasLinearTextureFormatFeatureBits(VkFormat format,
1784 const VkFormatFeatureFlags featureBits)
1785{
1786 return hasFormatFeatureBits<&VkFormatProperties::linearTilingFeatures>(format, featureBits);
1787}
1788
1789bool RendererVk::hasTextureFormatFeatureBits(VkFormat format,
1790 const VkFormatFeatureFlags featureBits)
1791{
1792 return hasFormatFeatureBits<&VkFormatProperties::optimalTilingFeatures>(format, featureBits);
1793}
1794
1795bool RendererVk::hasBufferFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
1796{
1797 return hasFormatFeatureBits<&VkFormatProperties::bufferFeatures>(format, featureBits);
1798}
1799
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001800angle::Result RendererVk::synchronizeCpuGpuTime(vk::Context *context)
1801{
1802 ASSERT(mGpuEventsEnabled);
1803
1804 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1805 ASSERT(platform);
1806
1807 // To synchronize CPU and GPU times, we need to get the CPU timestamp as close as possible to
1808 // the GPU timestamp. The process of getting the GPU timestamp is as follows:
1809 //
1810 // CPU GPU
1811 //
1812 // Record command buffer
1813 // with timestamp query
1814 //
1815 // Submit command buffer
1816 //
1817 // Post-submission work Begin execution
1818 //
Yuly Novikov0546b532019-02-25 22:47:17 +00001819 // ???? Write timstamp Tgpu
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001820 //
1821 // ???? End execution
1822 //
1823 // ???? Return query results
1824 //
1825 // ????
1826 //
1827 // Get query results
1828 //
1829 // The areas of unknown work (????) on the CPU indicate that the CPU may or may not have
1830 // finished post-submission work while the GPU is executing in parallel. With no further work,
1831 // querying CPU timestamps before submission and after getting query results give the bounds to
1832 // Tgpu, which could be quite large.
1833 //
1834 // Using VkEvents, the GPU can be made to wait for the CPU and vice versa, in an effort to
1835 // reduce this range. This function implements the following procedure:
1836 //
1837 // CPU GPU
1838 //
1839 // Record command buffer
1840 // with timestamp query
1841 //
1842 // Submit command buffer
1843 //
1844 // Post-submission work Begin execution
1845 //
1846 // ???? Set Event GPUReady
1847 //
1848 // Wait on Event GPUReady Wait on Event CPUReady
1849 //
1850 // Get CPU Time Ts Wait on Event CPUReady
1851 //
1852 // Set Event CPUReady Wait on Event CPUReady
1853 //
1854 // Get CPU Time Tcpu Get GPU Time Tgpu
1855 //
1856 // Wait on Event GPUDone Set Event GPUDone
1857 //
1858 // Get CPU Time Te End Execution
1859 //
1860 // Idle Return query results
1861 //
1862 // Get query results
1863 //
1864 // If Te-Ts > epsilon, a GPU or CPU interruption can be assumed and the operation can be
1865 // retried. Once Te-Ts < epsilon, Tcpu can be taken to presumably match Tgpu. Finding an
1866 // epsilon that's valid for all devices may be difficult, so the loop can be performed only a
1867 // limited number of times and the Tcpu,Tgpu pair corresponding to smallest Te-Ts used for
1868 // calibration.
1869 //
1870 // Note: Once VK_EXT_calibrated_timestamps is ubiquitous, this should be redone.
1871
1872 // Make sure nothing is running
1873 ASSERT(mCommandGraph.empty());
1874
1875 TRACE_EVENT0("gpu.angle", "RendererVk::synchronizeCpuGpuTime");
1876
1877 // Create a query used to receive the GPU timestamp
1878 vk::QueryHelper timestampQuery;
1879 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &timestampQuery));
1880
1881 // Create the three events
1882 VkEventCreateInfo eventCreateInfo = {};
1883 eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
1884 eventCreateInfo.flags = 0;
1885
1886 vk::Scoped<vk::Event> cpuReady(mDevice), gpuReady(mDevice), gpuDone(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001887 ANGLE_VK_TRY(context, cpuReady.get().init(mDevice, eventCreateInfo));
1888 ANGLE_VK_TRY(context, gpuReady.get().init(mDevice, eventCreateInfo));
1889 ANGLE_VK_TRY(context, gpuDone.get().init(mDevice, eventCreateInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001890
1891 constexpr uint32_t kRetries = 10;
1892
1893 // Time suffixes used are S for seconds and Cycles for cycles
1894 double tightestRangeS = 1e6f;
1895 double TcpuS = 0;
1896 uint64_t TgpuCycles = 0;
1897 for (uint32_t i = 0; i < kRetries; ++i)
1898 {
1899 // Reset the events
Yuly Novikov27780292018-11-09 11:19:49 -05001900 ANGLE_VK_TRY(context, cpuReady.get().reset(mDevice));
1901 ANGLE_VK_TRY(context, gpuReady.get().reset(mDevice));
1902 ANGLE_VK_TRY(context, gpuDone.get().reset(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001903
1904 // Record the command buffer
1905 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1906 vk::CommandBuffer &commandBuffer = commandBatch.get();
1907
1908 VkCommandBufferAllocateInfo commandBufferInfo = {};
1909 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1910 commandBufferInfo.commandPool = mCommandPool.getHandle();
1911 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1912 commandBufferInfo.commandBufferCount = 1;
1913
Yuly Novikov27780292018-11-09 11:19:49 -05001914 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001915
1916 VkCommandBufferBeginInfo beginInfo = {};
1917 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1918 beginInfo.flags = 0;
1919 beginInfo.pInheritanceInfo = nullptr;
1920
Yuly Novikov27780292018-11-09 11:19:49 -05001921 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001922
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001923 commandBuffer.setEvent(gpuReady.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001924 commandBuffer.waitEvents(1, cpuReady.get().ptr(), VK_PIPELINE_STAGE_HOST_BIT,
1925 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, nullptr, 0, nullptr, 0,
1926 nullptr);
1927
1928 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1929 timestampQuery.getQuery(), 1);
1930 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1931 timestampQuery.getQueryPool()->getHandle(),
1932 timestampQuery.getQuery());
1933
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001934 commandBuffer.setEvent(gpuDone.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001935
Yuly Novikov27780292018-11-09 11:19:49 -05001936 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001937
1938 // Submit the command buffer
1939 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1940 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1941 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
1942
1943 VkSubmitInfo submitInfo = {};
1944 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1945 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1946 submitInfo.pWaitSemaphores = waitSemaphores.data();
1947 submitInfo.pWaitDstStageMask = waitStageMasks.data();
1948 submitInfo.commandBufferCount = 1;
1949 submitInfo.pCommandBuffers = commandBuffer.ptr();
1950 submitInfo.signalSemaphoreCount = 0;
1951 submitInfo.pSignalSemaphores = nullptr;
1952
1953 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBuffer)));
1954
1955 // Wait for GPU to be ready. This is a short busy wait.
Yuly Novikov27780292018-11-09 11:19:49 -05001956 VkResult result = VK_EVENT_RESET;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001957 do
1958 {
Yuly Novikov27780292018-11-09 11:19:49 -05001959 result = gpuReady.get().getStatus(mDevice);
1960 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
1961 {
1962 ANGLE_VK_TRY(context, result);
1963 }
1964 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001965
1966 double TsS = platform->monotonicallyIncreasingTime(platform);
1967
1968 // Tell the GPU to go ahead with the timestamp query.
Yuly Novikov27780292018-11-09 11:19:49 -05001969 ANGLE_VK_TRY(context, cpuReady.get().set(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001970 double cpuTimestampS = platform->monotonicallyIncreasingTime(platform);
1971
1972 // Wait for GPU to be done. Another short busy wait.
1973 do
1974 {
Yuly Novikov27780292018-11-09 11:19:49 -05001975 result = gpuDone.get().getStatus(mDevice);
1976 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
1977 {
1978 ANGLE_VK_TRY(context, result);
1979 }
1980 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001981
1982 double TeS = platform->monotonicallyIncreasingTime(platform);
1983
1984 // Get the query results
1985 ANGLE_TRY(finishToSerial(context, getLastSubmittedQueueSerial()));
1986
1987 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1988
1989 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05001990 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
1991 mDevice, timestampQuery.getQuery(), 1, sizeof(gpuTimestampCycles),
1992 &gpuTimestampCycles, sizeof(gpuTimestampCycles), queryFlags));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001993
1994 // Use the first timestamp queried as origin.
1995 if (mGpuEventTimestampOrigin == 0)
1996 {
1997 mGpuEventTimestampOrigin = gpuTimestampCycles;
1998 }
1999
2000 // Take these CPU and GPU timestamps if there is better confidence.
2001 double confidenceRangeS = TeS - TsS;
2002 if (confidenceRangeS < tightestRangeS)
2003 {
2004 tightestRangeS = confidenceRangeS;
2005 TcpuS = cpuTimestampS;
2006 TgpuCycles = gpuTimestampCycles;
2007 }
2008 }
2009
2010 mGpuEventQueryPool.freeQuery(context, &timestampQuery);
2011
2012 // timestampPeriod gives nanoseconds/cycle.
2013 double TgpuS = (TgpuCycles - mGpuEventTimestampOrigin) *
2014 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) /
2015 1'000'000'000.0;
2016
2017 flushGpuEvents(TgpuS, TcpuS);
2018
2019 mGpuClockSync.gpuTimestampS = TgpuS;
2020 mGpuClockSync.cpuTimestampS = TcpuS;
2021
Jamie Madill7c985f52018-11-29 18:16:17 -05002022 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002023}
2024
2025angle::Result RendererVk::traceGpuEventImpl(vk::Context *context,
2026 vk::CommandBuffer *commandBuffer,
2027 char phase,
2028 const char *name)
2029{
2030 ASSERT(mGpuEventsEnabled);
2031
2032 GpuEventQuery event;
2033
2034 event.name = name;
2035 event.phase = phase;
2036 event.serial = mCurrentQueueSerial;
2037
2038 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &event.queryPoolIndex, &event.queryIndex));
2039
2040 commandBuffer->resetQueryPool(
2041 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex, 1);
2042 commandBuffer->writeTimestamp(
2043 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
2044 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex);
2045
2046 mInFlightGpuEventQueries.push_back(std::move(event));
2047
Jamie Madill7c985f52018-11-29 18:16:17 -05002048 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002049}
2050
2051angle::Result RendererVk::checkCompletedGpuEvents(vk::Context *context)
2052{
2053 ASSERT(mGpuEventsEnabled);
2054
2055 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
2056 ASSERT(platform);
2057
2058 int finishedCount = 0;
2059
2060 for (GpuEventQuery &eventQuery : mInFlightGpuEventQueries)
2061 {
2062 // Only check the timestamp query if the submission has finished.
2063 if (eventQuery.serial > mLastCompletedQueueSerial)
2064 {
2065 break;
2066 }
2067
2068 // See if the results are available.
2069 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05002070 VkResult result = mGpuEventQueryPool.getQueryPool(eventQuery.queryPoolIndex)
2071 ->getResults(mDevice, eventQuery.queryIndex, 1,
2072 sizeof(gpuTimestampCycles), &gpuTimestampCycles,
2073 sizeof(gpuTimestampCycles), VK_QUERY_RESULT_64_BIT);
2074 if (result == VK_NOT_READY)
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002075 {
2076 break;
2077 }
Yuly Novikov27780292018-11-09 11:19:49 -05002078 ANGLE_VK_TRY(context, result);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002079
2080 mGpuEventQueryPool.freeQuery(context, eventQuery.queryPoolIndex, eventQuery.queryIndex);
2081
2082 GpuEvent event;
2083 event.gpuTimestampCycles = gpuTimestampCycles;
2084 event.name = eventQuery.name;
2085 event.phase = eventQuery.phase;
2086
2087 mGpuEvents.emplace_back(event);
2088
2089 ++finishedCount;
2090 }
2091
2092 mInFlightGpuEventQueries.erase(mInFlightGpuEventQueries.begin(),
2093 mInFlightGpuEventQueries.begin() + finishedCount);
2094
Jamie Madill7c985f52018-11-29 18:16:17 -05002095 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002096}
2097
2098void RendererVk::flushGpuEvents(double nextSyncGpuTimestampS, double nextSyncCpuTimestampS)
2099{
2100 if (mGpuEvents.size() == 0)
2101 {
2102 return;
2103 }
2104
2105 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
2106 ASSERT(platform);
2107
2108 // Find the slope of the clock drift for adjustment
2109 double lastGpuSyncTimeS = mGpuClockSync.gpuTimestampS;
2110 double lastGpuSyncDiffS = mGpuClockSync.cpuTimestampS - mGpuClockSync.gpuTimestampS;
2111 double gpuSyncDriftSlope = 0;
2112
2113 double nextGpuSyncTimeS = nextSyncGpuTimestampS;
2114 double nextGpuSyncDiffS = nextSyncCpuTimestampS - nextSyncGpuTimestampS;
2115
2116 // No gpu trace events should have been generated before the clock sync, so if there is no
2117 // "previous" clock sync, there should be no gpu events (i.e. the function early-outs above).
2118 ASSERT(mGpuClockSync.gpuTimestampS != std::numeric_limits<double>::max() &&
2119 mGpuClockSync.cpuTimestampS != std::numeric_limits<double>::max());
2120
2121 gpuSyncDriftSlope =
2122 (nextGpuSyncDiffS - lastGpuSyncDiffS) / (nextGpuSyncTimeS - lastGpuSyncTimeS);
2123
2124 for (const GpuEvent &event : mGpuEvents)
2125 {
2126 double gpuTimestampS =
2127 (event.gpuTimestampCycles - mGpuEventTimestampOrigin) *
2128 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) * 1e-9;
2129
2130 // Account for clock drift.
2131 gpuTimestampS += lastGpuSyncDiffS + gpuSyncDriftSlope * (gpuTimestampS - lastGpuSyncTimeS);
2132
2133 // Generate the trace now that the GPU timestamp is available and clock drifts are accounted
2134 // for.
2135 static long long eventId = 1;
2136 static const unsigned char *categoryEnabled =
2137 TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu.angle.gpu");
2138 platform->addTraceEvent(platform, event.phase, categoryEnabled, event.name, eventId++,
2139 gpuTimestampS, 0, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
2140 }
2141
2142 mGpuEvents.clear();
2143}
2144
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05002145template <VkFormatFeatureFlags VkFormatProperties::*features>
2146bool RendererVk::hasFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
2147{
2148 ASSERT(static_cast<uint32_t>(format) < vk::kNumVkFormats);
2149 VkFormatProperties &deviceProperties = mFormatProperties[format];
2150
2151 if (deviceProperties.bufferFeatures == kInvalidFormatFeatureFlags)
2152 {
2153 // If we don't have the actual device features, see if the requested features are mandatory.
2154 // If so, there's no need to query the device.
2155 const VkFormatProperties &mandatoryProperties = vk::GetMandatoryFormatSupport(format);
2156 if (IsMaskFlagSet(mandatoryProperties.*features, featureBits))
2157 {
2158 return true;
2159 }
2160
2161 // Otherwise query the format features and cache it.
2162 vkGetPhysicalDeviceFormatProperties(mPhysicalDevice, format, &deviceProperties);
2163 }
2164
2165 return IsMaskFlagSet(deviceProperties.*features, featureBits);
2166}
2167
Jamie Madillaaca96e2018-06-12 10:19:48 -04002168uint32_t GetUniformBufferDescriptorCount()
2169{
2170 return kUniformBufferDescriptorsPerDescriptorSet;
2171}
2172
Jamie Madill9e54b5a2016-05-25 12:57:39 -04002173} // namespace rx