blob: 177c2f3d5b9941140c8e80a6c51a00338fcffbc7 [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 -050082bool ExtensionFound(const char *needle, const RendererVk::ExtensionNameList &haystack)
83{
84 // NOTE: The list must be sorted.
85 return std::binary_search(haystack.begin(), haystack.end(), needle, StrLess);
Tobin Ehlis3a181e32018-08-29 15:17:05 -060086}
87
Yuly Novikovdbbdf562019-02-27 19:48:16 -050088VkResult VerifyExtensionsPresent(const RendererVk::ExtensionNameList &haystack,
89 const RendererVk::ExtensionNameList &needles)
90{
91 // NOTE: The lists must be sorted.
92 if (std::includes(haystack.begin(), haystack.end(), needles.begin(), needles.end(), StrLess))
93 {
94 return VK_SUCCESS;
95 }
96 for (const char *needle : needles)
97 {
98 if (!ExtensionFound(needle, haystack))
99 {
100 ERR() << "Extension not supported: " << needle;
101 }
102 }
103 return VK_ERROR_EXTENSION_NOT_PRESENT;
104}
105
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500106// Array of Validation error/warning messages that will be ignored, should include bugID
Jamie Madill00f43c92019-02-09 11:41:12 -0500107constexpr const char *kSkippedMessages[] = {
108 // http://anglebug.com/2866
109 "UNASSIGNED-CoreValidation-Shader-OutputNotConsumed",
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500110 // http://anglebug.com/2796
Jamie Madill00f43c92019-02-09 11:41:12 -0500111 "UNASSIGNED-CoreValidation-Shader-PointSizeMissing",
112};
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500113
114// Suppress validation errors that are known
115// return "true" if given code/prefix/message is known, else return "false"
116bool IsIgnoredDebugMessage(const char *message)
117{
Michael Spang25839802019-01-30 18:02:51 -0500118 if (!message)
119 {
120 return false;
121 }
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500122 for (const char *msg : kSkippedMessages)
123 {
124 if (strstr(message, msg) != nullptr)
125 {
126 return true;
127 }
128 }
129 return false;
130}
131
132const char *GetVkObjectTypeName(VkObjectType type)
133{
134 switch (type)
135 {
136 case VK_OBJECT_TYPE_UNKNOWN:
137 return "Unknown";
138 case VK_OBJECT_TYPE_INSTANCE:
139 return "Instance";
140 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
141 return "Physical Device";
142 case VK_OBJECT_TYPE_DEVICE:
143 return "Device";
144 case VK_OBJECT_TYPE_QUEUE:
145 return "Queue";
146 case VK_OBJECT_TYPE_SEMAPHORE:
147 return "Semaphore";
148 case VK_OBJECT_TYPE_COMMAND_BUFFER:
149 return "Command Buffer";
150 case VK_OBJECT_TYPE_FENCE:
151 return "Fence";
152 case VK_OBJECT_TYPE_DEVICE_MEMORY:
153 return "Device Memory";
154 case VK_OBJECT_TYPE_BUFFER:
155 return "Buffer";
156 case VK_OBJECT_TYPE_IMAGE:
157 return "Image";
158 case VK_OBJECT_TYPE_EVENT:
159 return "Event";
160 case VK_OBJECT_TYPE_QUERY_POOL:
161 return "Query Pool";
162 case VK_OBJECT_TYPE_BUFFER_VIEW:
163 return "Buffer View";
164 case VK_OBJECT_TYPE_IMAGE_VIEW:
165 return "Image View";
166 case VK_OBJECT_TYPE_SHADER_MODULE:
167 return "Shader Module";
168 case VK_OBJECT_TYPE_PIPELINE_CACHE:
169 return "Pipeline Cache";
170 case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
171 return "Pipeline Layout";
172 case VK_OBJECT_TYPE_RENDER_PASS:
173 return "Render Pass";
174 case VK_OBJECT_TYPE_PIPELINE:
175 return "Pipeline";
176 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
177 return "Descriptor Set Layout";
178 case VK_OBJECT_TYPE_SAMPLER:
179 return "Sampler";
180 case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
181 return "Descriptor Pool";
182 case VK_OBJECT_TYPE_DESCRIPTOR_SET:
183 return "Descriptor Set";
184 case VK_OBJECT_TYPE_FRAMEBUFFER:
185 return "Framebuffer";
186 case VK_OBJECT_TYPE_COMMAND_POOL:
187 return "Command Pool";
188 case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION:
189 return "Sampler YCbCr Conversion";
190 case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE:
191 return "Descriptor Update Template";
192 case VK_OBJECT_TYPE_SURFACE_KHR:
193 return "Surface";
194 case VK_OBJECT_TYPE_SWAPCHAIN_KHR:
195 return "Swapchain";
196 case VK_OBJECT_TYPE_DISPLAY_KHR:
197 return "Display";
198 case VK_OBJECT_TYPE_DISPLAY_MODE_KHR:
199 return "Display Mode";
200 case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT:
201 return "Debug Report Callback";
202 case VK_OBJECT_TYPE_OBJECT_TABLE_NVX:
203 return "Object Table";
204 case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX:
205 return "Indirect Commands Layout";
206 case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT:
207 return "Debug Utils Messenger";
208 case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT:
209 return "Validation Cache";
Shahbaz Youssefi6f1a8522019-02-26 15:00:54 +0000210 case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV:
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500211 return "Acceleration Structure";
212 default:
213 return "<Unrecognized>";
214 }
215}
216
217VKAPI_ATTR VkBool32 VKAPI_CALL
218DebugUtilsMessenger(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
219 VkDebugUtilsMessageTypeFlagsEXT messageTypes,
220 const VkDebugUtilsMessengerCallbackDataEXT *callbackData,
221 void *userData)
222{
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500223 // See if it's an issue we are aware of and don't want to be spammed about.
224 if (IsIgnoredDebugMessage(callbackData->pMessageIdName))
225 {
226 return VK_FALSE;
227 }
228
229 std::ostringstream log;
Michael Spang25839802019-01-30 18:02:51 -0500230 if (callbackData->pMessageIdName)
231 {
232 log << "[ " << callbackData->pMessageIdName << " ] ";
233 }
234 log << callbackData->pMessage << std::endl;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500235
236 // Aesthetic value based on length of the function name, line number, etc.
237 constexpr size_t kStartIndent = 28;
238
239 // Output the debug marker hierarchy under which this error has occured.
240 size_t indent = kStartIndent;
241 if (callbackData->queueLabelCount > 0)
242 {
243 log << std::string(indent++, ' ') << "<Queue Label Hierarchy:>" << std::endl;
244 for (uint32_t i = 0; i < callbackData->queueLabelCount; ++i)
245 {
246 log << std::string(indent++, ' ') << callbackData->pQueueLabels[i].pLabelName
247 << std::endl;
248 }
249 }
250 if (callbackData->cmdBufLabelCount > 0)
251 {
252 log << std::string(indent++, ' ') << "<Command Buffer Label Hierarchy:>" << std::endl;
253 for (uint32_t i = 0; i < callbackData->cmdBufLabelCount; ++i)
254 {
255 log << std::string(indent++, ' ') << callbackData->pCmdBufLabels[i].pLabelName
256 << std::endl;
257 }
258 }
259 // Output the objects involved in this error message.
260 if (callbackData->objectCount > 0)
261 {
262 for (uint32_t i = 0; i < callbackData->objectCount; ++i)
263 {
264 const char *objectName = callbackData->pObjects[i].pObjectName;
265 const char *objectType = GetVkObjectTypeName(callbackData->pObjects[i].objectType);
266 uint64_t objectHandle = callbackData->pObjects[i].objectHandle;
267 log << std::string(indent, ' ') << "Object: ";
268 if (objectHandle == 0)
269 {
270 log << "VK_NULL_HANDLE";
271 }
272 else
273 {
274 log << "0x" << std::hex << objectHandle << std::dec;
275 }
276 log << " (type = " << objectType << "(" << callbackData->pObjects[i].objectType << "))";
277 if (objectName)
278 {
279 log << " [" << objectName << "]";
280 }
281 log << std::endl;
282 }
283 }
284
Jamie Madill54ed8f02019-02-11 12:32:04 -0500285 bool isError = (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0;
286 std::string msg = log.str();
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500287
288 if (isError)
289 {
Jamie Madill54ed8f02019-02-11 12:32:04 -0500290 ERR() << msg;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500291 }
292 else
293 {
Jamie Madill54ed8f02019-02-11 12:32:04 -0500294 WARN() << msg;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500295 }
296
297 return VK_FALSE;
298}
299
Yuly Novikov199f4292018-01-19 19:04:05 -0500300VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
301 VkDebugReportObjectTypeEXT objectType,
302 uint64_t object,
303 size_t location,
304 int32_t messageCode,
305 const char *layerPrefix,
306 const char *message,
307 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500308{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600309 if (IsIgnoredDebugMessage(message))
310 {
311 return VK_FALSE;
312 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500313 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
314 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500315 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500316#if !defined(NDEBUG)
317 // Abort the call in Debug builds.
318 return VK_TRUE;
319#endif
320 }
321 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
322 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500323 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500324 }
325 else
326 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500327 // Uncomment this if you want Vulkan spam.
328 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500329 }
330
331 return VK_FALSE;
332}
333
Yuly Novikov199f4292018-01-19 19:04:05 -0500334// If we're loading the validation layers, we could be running from any random directory.
335// Change to the executable directory so we can find the layers, then change back to the
336// previous directory to be safe we don't disrupt the application.
337class ScopedVkLoaderEnvironment : angle::NonCopyable
338{
339 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600340 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
341 : mEnableValidationLayers(enableValidationLayers),
342 mEnableMockICD(enableMockICD),
343 mChangedCWD(false),
344 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500345 {
346// Changing CWD and setting environment variables makes no sense on Android,
347// since this code is a part of Java application there.
348// Android Vulkan loader doesn't need this either.
Michael Spang229fc832019-01-21 18:09:15 -0500349#if !defined(ANGLE_PLATFORM_ANDROID) && !defined(ANGLE_PLATFORM_FUCHSIA)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600350 if (enableMockICD)
351 {
352 // Override environment variable to use built Mock ICD
353 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
354 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
355 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
356 if (!mChangedICDPath)
357 {
358 ERR() << "Error setting Path for Mock/Null Driver.";
359 mEnableMockICD = false;
360 }
361 }
Jamie Madill46848422018-08-09 10:46:06 -0400362 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500363 {
364 const auto &cwd = angle::GetCWD();
365 if (!cwd.valid())
366 {
367 ERR() << "Error getting CWD for Vulkan layers init.";
368 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400369 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500370 }
371 else
372 {
373 mPreviousCWD = cwd.value();
Jamie Madillbab03022019-01-16 14:12:28 -0500374 std::string exeDir = angle::GetExecutableDirectory();
375 mChangedCWD = angle::SetCWD(exeDir.c_str());
Yuly Novikov199f4292018-01-19 19:04:05 -0500376 if (!mChangedCWD)
377 {
378 ERR() << "Error setting CWD for Vulkan layers init.";
379 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400380 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500381 }
382 }
383 }
384
385 // Override environment variable to use the ANGLE layers.
386 if (mEnableValidationLayers)
387 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700388 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500389 {
390 ERR() << "Error setting environment for Vulkan layers init.";
391 mEnableValidationLayers = false;
392 }
393 }
394#endif // !defined(ANGLE_PLATFORM_ANDROID)
395 }
396
397 ~ScopedVkLoaderEnvironment()
398 {
399 if (mChangedCWD)
400 {
401#if !defined(ANGLE_PLATFORM_ANDROID)
402 ASSERT(mPreviousCWD.valid());
403 angle::SetCWD(mPreviousCWD.value().c_str());
404#endif // !defined(ANGLE_PLATFORM_ANDROID)
405 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600406 if (mChangedICDPath)
407 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600408 if (mPreviousICDPath.value().empty())
409 {
410 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
411 }
412 else
413 {
414 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
415 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600416 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500417 }
418
Jamie Madillaaca96e2018-06-12 10:19:48 -0400419 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500420
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600421 bool canEnableMockICD() const { return mEnableMockICD; }
422
Yuly Novikov199f4292018-01-19 19:04:05 -0500423 private:
424 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600425 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500426 bool mChangedCWD;
427 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600428 bool mChangedICDPath;
429 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500430};
431
Jamie Madill21061022018-07-12 23:56:30 -0400432void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
433 bool preferMockICD,
434 VkPhysicalDevice *physicalDeviceOut,
435 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
436{
437 ASSERT(!physicalDevices.empty());
438 if (preferMockICD)
439 {
440 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
441 {
442 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
443 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
444 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
445 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
446 {
447 *physicalDeviceOut = physicalDevice;
448 return;
449 }
450 }
451 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
452 "physicalDevice instead.";
453 }
454
455 // Fall back to first device.
456 *physicalDeviceOut = physicalDevices[0];
457 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
458}
Jamie Madill0da73fe2018-10-02 09:31:39 -0400459
460// Initially dumping the command graphs is disabled.
461constexpr bool kEnableCommandGraphDiagnostics = false;
Ian Elliottbcb78902018-12-19 11:46:29 -0700462
Jamie Madille09bd5d2016-11-29 16:20:35 -0500463} // anonymous namespace
464
Jamie Madill49ac74b2017-12-21 14:42:33 -0500465// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400466RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500467
Jamie Madillaaca96e2018-06-12 10:19:48 -0400468RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500469
470RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000471 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
Jamie Madillb980c562018-11-27 11:34:27 -0500472{}
Jamie Madill49ac74b2017-12-21 14:42:33 -0500473
474RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
475{
476 std::swap(commandPool, other.commandPool);
477 std::swap(fence, other.fence);
478 std::swap(serial, other.serial);
479 return *this;
480}
481
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000482void RendererVk::CommandBatch::destroy(VkDevice device)
Jamie Madillbea35a62018-07-05 11:54:10 -0400483{
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000484 commandPool.destroy(device);
Jamie Madillbea35a62018-07-05 11:54:10 -0400485 fence.destroy(device);
486}
487
Jamie Madill9f2a8612017-11-30 12:43:09 -0500488// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500489RendererVk::RendererVk()
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400490 : mDisplay(nullptr),
491 mCapsInitialized(false),
Ian Elliottbcb78902018-12-19 11:46:29 -0700492 mFeaturesInitialized(false),
Jamie Madill0448ec82016-12-23 13:41:47 -0500493 mInstance(VK_NULL_HANDLE),
494 mEnableValidationLayers(false),
Jamie Madill0ea96212018-10-30 15:14:51 -0400495 mEnableMockICD(false),
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500496 mDebugUtilsMessenger(VK_NULL_HANDLE),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500497 mDebugReportCallback(VK_NULL_HANDLE),
498 mPhysicalDevice(VK_NULL_HANDLE),
499 mQueue(VK_NULL_HANDLE),
500 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
Frank Henigman52047de2018-11-13 17:22:36 -0500501 mMaxVertexAttribDivisor(1),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500502 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400503 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400504 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400505 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400506 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400507 mCommandGraph(kEnableCommandGraphDiagnostics),
508 mGpuEventsEnabled(false),
509 mGpuClockSync{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()},
510 mGpuEventTimestampOrigin(0)
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500511{
512 VkFormatProperties invalid = {0, 0, kInvalidFormatFeatureFlags};
513 mFormatProperties.fill(invalid);
514}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400515
Jamie Madillb980c562018-11-27 11:34:27 -0500516RendererVk::~RendererVk() {}
Jamie Madill21061022018-07-12 23:56:30 -0400517
518void RendererVk::onDestroy(vk::Context *context)
519{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500520 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500521 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500522 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400523 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500524 }
525
Shahbaz Youssefie3219402018-12-08 16:54:14 +0100526 mUtils.destroy(mDevice);
Shahbaz Youssefi8f1b7a62018-11-14 16:02:54 -0500527
Jamie Madillc7918ce2018-06-13 13:25:31 -0400528 mPipelineLayoutCache.destroy(mDevice);
529 mDescriptorSetLayoutCache.destroy(mDevice);
530
Jamie Madill9f2a8612017-11-30 12:43:09 -0500531 mRenderPassCache.destroy(mDevice);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500532 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400533 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400534 mShaderLibrary.destroy(mDevice);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400535 mGpuEventQueryPool.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500536
Jamie Madill06ca6342018-07-12 15:56:53 -0400537 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500538
Jamie Madill5deea722017-02-16 10:44:46 -0500539 if (mCommandPool.valid())
540 {
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000541 mCommandPool.destroy(mDevice);
Jamie Madill5deea722017-02-16 10:44:46 -0500542 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500543
544 if (mDevice)
545 {
546 vkDestroyDevice(mDevice, nullptr);
547 mDevice = VK_NULL_HANDLE;
548 }
549
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500550 if (mDebugUtilsMessenger)
Jamie Madill0448ec82016-12-23 13:41:47 -0500551 {
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500552 ASSERT(mInstance && vkDestroyDebugUtilsMessengerEXT);
553 vkDestroyDebugUtilsMessengerEXT(mInstance, mDebugUtilsMessenger, nullptr);
554
555 ASSERT(mDebugReportCallback == VK_NULL_HANDLE);
556 }
557 else if (mDebugReportCallback)
558 {
559 ASSERT(mInstance && vkDestroyDebugReportCallbackEXT);
560 vkDestroyDebugReportCallbackEXT(mInstance, mDebugReportCallback, nullptr);
Jamie Madill0448ec82016-12-23 13:41:47 -0500561 }
562
Jamie Madill4d0bf552016-12-28 15:45:24 -0500563 if (mInstance)
564 {
565 vkDestroyInstance(mInstance, nullptr);
566 mInstance = VK_NULL_HANDLE;
567 }
568
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600569 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500570 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500571}
572
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400573void RendererVk::notifyDeviceLost()
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400574{
575 mDeviceLost = true;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400576
577 mCommandGraph.clear();
Jamie Madill85ca1892019-01-16 13:27:15 -0500578 nextSerial();
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400579 freeAllInFlightResources();
580
581 mDisplay->notifyDeviceLost();
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400582}
583
584bool RendererVk::isDeviceLost() const
585{
586 return mDeviceLost;
587}
588
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400589angle::Result RendererVk::initialize(DisplayVk *displayVk,
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400590 egl::Display *display,
Michael Spang740db7f2019-02-06 09:40:13 -0500591 const char *wsiExtension,
592 const char *wsiLayer)
Jamie Madill327ba852016-11-30 12:38:28 -0500593{
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400594 mDisplay = display;
595 const egl::AttributeMap &attribs = mDisplay->getAttributeMap();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600596 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
597 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500598 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madill0ea96212018-10-30 15:14:51 -0400599 mEnableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500600
Jamie Madill0448ec82016-12-23 13:41:47 -0500601 // Gather global layer properties.
602 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400603 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500604
605 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
606 if (instanceLayerCount > 0)
607 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400608 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
609 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500610 }
611
Michael Spang6c13c702019-02-06 15:59:44 -0500612 VulkanLayerVector enabledInstanceLayerNames;
613 if (mEnableValidationLayers)
614 {
615 bool layersRequested =
616 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
617 mEnableValidationLayers = GetAvailableValidationLayers(instanceLayerProps, layersRequested,
618 &enabledInstanceLayerNames);
619 }
620
621 if (wsiLayer)
622 {
623 enabledInstanceLayerNames.push_back(wsiLayer);
624 }
625
626 // Enumerate instance extensions that are provided by the vulkan
627 // implementation and implicit layers.
Jamie Madille09bd5d2016-11-29 16:20:35 -0500628 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400629 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400630 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500631
632 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
633 if (instanceExtensionCount > 0)
634 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400635 ANGLE_VK_TRY(displayVk,
636 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
637 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500638 }
639
Michael Spang6c13c702019-02-06 15:59:44 -0500640 // Enumerate instance extensions that are provided by explicit layers.
641 for (const char *layerName : enabledInstanceLayerNames)
Jamie Madill0448ec82016-12-23 13:41:47 -0500642 {
Michael Spang6c13c702019-02-06 15:59:44 -0500643 uint32_t previousExtensionCount = instanceExtensionProps.size();
644 uint32_t instanceLayerExtensionCount = 0;
645 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties(
646 layerName, &instanceLayerExtensionCount, nullptr));
647 instanceExtensionProps.resize(previousExtensionCount + instanceLayerExtensionCount);
648 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties(
649 layerName, &instanceLayerExtensionCount,
650 instanceExtensionProps.data() + previousExtensionCount));
Jamie Madill0448ec82016-12-23 13:41:47 -0500651 }
652
Frank Henigman52047de2018-11-13 17:22:36 -0500653 ExtensionNameList instanceExtensionNames;
654 if (!instanceExtensionProps.empty())
655 {
656 for (const VkExtensionProperties &i : instanceExtensionProps)
657 {
658 instanceExtensionNames.push_back(i.extensionName);
659 }
660 std::sort(instanceExtensionNames.begin(), instanceExtensionNames.end(), StrLess);
661 }
662
663 ExtensionNameList enabledInstanceExtensions;
Jamie Madille09bd5d2016-11-29 16:20:35 -0500664 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Michael Spang740db7f2019-02-06 09:40:13 -0500665 enabledInstanceExtensions.push_back(wsiExtension);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500666
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500667 bool enableDebugUtils =
668 mEnableValidationLayers &&
Frank Henigman52047de2018-11-13 17:22:36 -0500669 ExtensionFound(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instanceExtensionNames);
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500670 bool enableDebugReport =
671 mEnableValidationLayers && !enableDebugUtils &&
Frank Henigman52047de2018-11-13 17:22:36 -0500672 ExtensionFound(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instanceExtensionNames);
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500673
674 if (enableDebugUtils)
675 {
676 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
677 }
678 else if (enableDebugReport)
Jamie Madill0448ec82016-12-23 13:41:47 -0500679 {
680 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
681 }
682
Jamie Madille09bd5d2016-11-29 16:20:35 -0500683 // Verify the required extensions are in the extension names set. Fail if not.
Frank Henigman52047de2018-11-13 17:22:36 -0500684 std::sort(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(), StrLess);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400685 ANGLE_VK_TRY(displayVk,
Frank Henigman52047de2018-11-13 17:22:36 -0500686 VerifyExtensionsPresent(instanceExtensionNames, enabledInstanceExtensions));
687
688 // Enable VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME if available.
689 if (ExtensionFound(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
690 instanceExtensionNames))
691 {
692 enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
693 }
Jamie Madille09bd5d2016-11-29 16:20:35 -0500694
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400695 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500696 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500697 applicationInfo.pApplicationName = "ANGLE";
698 applicationInfo.applicationVersion = 1;
699 applicationInfo.pEngineName = "ANGLE";
700 applicationInfo.engineVersion = 1;
Ian Elliott899c5d22018-12-21 13:12:50 -0700701
702 auto enumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
703 vkGetInstanceProcAddr(mInstance, "vkEnumerateInstanceVersion"));
704 if (!enumerateInstanceVersion)
705 {
706 applicationInfo.apiVersion = VK_API_VERSION_1_0;
707 }
708 else
709 {
710 uint32_t apiVersion = VK_API_VERSION_1_0;
711 ANGLE_VK_TRY(displayVk, enumerateInstanceVersion(&apiVersion));
712 if ((VK_VERSION_MAJOR(apiVersion) > 1) || (VK_VERSION_MINOR(apiVersion) >= 1))
713 {
Ian Elliott356d26c2019-02-11 11:27:01 -0700714 // This is the highest version of core Vulkan functionality that ANGLE uses.
715 applicationInfo.apiVersion = kPreferredVulkanAPIVersion;
Ian Elliott899c5d22018-12-21 13:12:50 -0700716 }
717 else
718 {
Ian Elliott356d26c2019-02-11 11:27:01 -0700719 // Since only 1.0 instance-level functionality is available, this must set to 1.0.
Ian Elliott899c5d22018-12-21 13:12:50 -0700720 applicationInfo.apiVersion = VK_API_VERSION_1_0;
721 }
722 }
Jamie Madill327ba852016-11-30 12:38:28 -0500723
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400724 VkInstanceCreateInfo instanceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -0500725 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
726 instanceInfo.flags = 0;
727 instanceInfo.pApplicationInfo = &applicationInfo;
Jamie Madill327ba852016-11-30 12:38:28 -0500728
Jamie Madille09bd5d2016-11-29 16:20:35 -0500729 // Enable requested layers and extensions.
730 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
731 instanceInfo.ppEnabledExtensionNames =
732 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Michael Spang6c13c702019-02-06 15:59:44 -0500733 instanceInfo.enabledLayerCount = enabledInstanceLayerNames.size();
734 instanceInfo.ppEnabledLayerNames = enabledInstanceLayerNames.data();
Jamie Madill327ba852016-11-30 12:38:28 -0500735
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400736 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500737
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500738 if (enableDebugUtils)
Jamie Madill0448ec82016-12-23 13:41:47 -0500739 {
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500740 // Try to use the newer EXT_debug_utils if it exists.
741 InitDebugUtilsEXTFunctions(mInstance);
742
743 // Create the messenger callback.
744 VkDebugUtilsMessengerCreateInfoEXT messengerInfo = {};
745
Shahbaz Youssefi4d153382019-02-26 15:08:11 +0000746 constexpr VkDebugUtilsMessageSeverityFlagsEXT kSeveritiesToLog =
747 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
748 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
749
750 constexpr VkDebugUtilsMessageTypeFlagsEXT kMessagesToLog =
751 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
752 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
753 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
754
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500755 messengerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
Shahbaz Youssefi4d153382019-02-26 15:08:11 +0000756 messengerInfo.messageSeverity = kSeveritiesToLog;
757 messengerInfo.messageType = kMessagesToLog;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500758 messengerInfo.pfnUserCallback = &DebugUtilsMessenger;
759 messengerInfo.pUserData = this;
760
761 ANGLE_VK_TRY(displayVk, vkCreateDebugUtilsMessengerEXT(mInstance, &messengerInfo, nullptr,
762 &mDebugUtilsMessenger));
763 }
764 else if (enableDebugReport)
765 {
766 // Fallback to EXT_debug_report.
767 InitDebugReportEXTFunctions(mInstance);
768
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400769 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500770
771 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500772 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500773 debugReportInfo.pfnCallback = &DebugReportCallback;
774 debugReportInfo.pUserData = this;
775
Shahbaz Youssefi5bca4fe2019-01-09 17:07:06 -0500776 ANGLE_VK_TRY(displayVk, vkCreateDebugReportCallbackEXT(mInstance, &debugReportInfo, nullptr,
777 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500778 }
779
Frank Henigman52047de2018-11-13 17:22:36 -0500780 if (std::find(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(),
781 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) !=
782 enabledInstanceExtensions.end())
783 {
784 InitGetPhysicalDeviceProperties2KHRFunctions(mInstance);
785 ASSERT(vkGetPhysicalDeviceProperties2KHR);
786 }
787
Jamie Madill4d0bf552016-12-28 15:45:24 -0500788 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400789 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
790 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500791
792 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700793 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400794 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
795 physicalDevices.data()));
Jamie Madill0ea96212018-10-30 15:14:51 -0400796 ChoosePhysicalDevice(physicalDevices, mEnableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700797 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500798
Jamie Madill30b5d842018-08-31 17:19:12 -0400799 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
800
Jamie Madill4d0bf552016-12-28 15:45:24 -0500801 // Ensure we can find a graphics queue family.
802 uint32_t queueCount = 0;
803 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
804
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400805 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500806
807 mQueueFamilyProperties.resize(queueCount);
808 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
809 mQueueFamilyProperties.data());
810
Jamie Madillb980c562018-11-27 11:34:27 -0500811 size_t graphicsQueueFamilyCount = false;
812 uint32_t firstGraphicsQueueFamily = 0;
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500813 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500814 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
815 {
816 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -0500817 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500818 {
819 ASSERT(queueInfo.queueCount > 0);
820 graphicsQueueFamilyCount++;
821 if (firstGraphicsQueueFamily == 0)
822 {
823 firstGraphicsQueueFamily = familyIndex;
824 }
825 break;
826 }
827 }
828
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400829 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500830
831 // If only one queue family, go ahead and initialize the device. If there is more than one
832 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
833 if (graphicsQueueFamilyCount == 1)
834 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400835 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500836 }
837
Jamie Madill035fd6b2017-10-03 15:43:22 -0400838 // Store the physical device memory properties so we can find the right memory pools.
839 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500840
Jamie Madill06ca6342018-07-12 15:56:53 -0400841 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500842
Jamie Madill6a89d222017-11-02 11:59:51 -0400843 // Initialize the format table.
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -0500844 mFormatTable.initialize(this, &mNativeTextureCaps, &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400845
Jamie Madill7c985f52018-11-29 18:16:17 -0500846 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400847}
848
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400849angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500850{
851 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400852 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400853 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500854
855 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
856 if (deviceLayerCount > 0)
857 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400858 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
859 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500860 }
861
Michael Spang6c13c702019-02-06 15:59:44 -0500862 VulkanLayerVector enabledDeviceLayerNames;
863 if (mEnableValidationLayers)
864 {
865 mEnableValidationLayers =
866 GetAvailableValidationLayers(deviceLayerProps, false, &enabledDeviceLayerNames);
867 }
868
869 const char *wsiLayer = displayVk->getWSILayer();
870 if (wsiLayer)
871 {
872 enabledDeviceLayerNames.push_back(wsiLayer);
873 }
874
875 // Enumerate device extensions that are provided by the vulkan
876 // implementation and implicit layers.
Jamie Madill4d0bf552016-12-28 15:45:24 -0500877 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400878 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
879 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500880
881 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
882 if (deviceExtensionCount > 0)
883 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400884 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
885 &deviceExtensionCount,
886 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500887 }
888
Michael Spang6c13c702019-02-06 15:59:44 -0500889 // Enumerate device extensions that are provided by explicit layers.
890 for (const char *layerName : enabledDeviceLayerNames)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500891 {
Michael Spang6c13c702019-02-06 15:59:44 -0500892 uint32_t previousExtensionCount = deviceExtensionProps.size();
893 uint32_t deviceLayerExtensionCount = 0;
894 ANGLE_VK_TRY(displayVk,
895 vkEnumerateDeviceExtensionProperties(mPhysicalDevice, layerName,
896 &deviceLayerExtensionCount, nullptr));
897 deviceExtensionProps.resize(previousExtensionCount + deviceLayerExtensionCount);
898 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(
899 mPhysicalDevice, layerName, &deviceLayerExtensionCount,
900 deviceExtensionProps.data() + previousExtensionCount));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500901 }
902
Frank Henigman52047de2018-11-13 17:22:36 -0500903 ExtensionNameList deviceExtensionNames;
904 if (!deviceExtensionProps.empty())
905 {
906 ASSERT(deviceExtensionNames.size() <= deviceExtensionProps.size());
907 for (const VkExtensionProperties &prop : deviceExtensionProps)
908 {
909 deviceExtensionNames.push_back(prop.extensionName);
910 }
911 std::sort(deviceExtensionNames.begin(), deviceExtensionNames.end(), StrLess);
912 }
913
914 ExtensionNameList enabledDeviceExtensions;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500915 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
916
Frank Henigman52047de2018-11-13 17:22:36 -0500917 initFeatures(deviceExtensionNames);
Ian Elliottbcb78902018-12-19 11:46:29 -0700918 mFeaturesInitialized = true;
919
Luc Ferronbf6dc372018-06-28 15:24:19 -0400920 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
Ian Elliott52f5da42018-12-21 09:02:09 -0700921 if ((getFeatures().flipViewportY) &&
922 (mPhysicalDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0)))
Luc Ferronbf6dc372018-06-28 15:24:19 -0400923 {
924 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
925 }
Ian Elliottbcb78902018-12-19 11:46:29 -0700926 if (getFeatures().supportsIncrementalPresent)
927 {
928 enabledDeviceExtensions.push_back(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME);
929 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400930
Geoff Lang009696c2019-01-31 14:47:07 -0500931#if defined(ANGLE_PLATFORM_ANDROID)
932 if (getFeatures().supportsAndroidHardwareBuffer)
933 {
934 enabledDeviceExtensions.push_back(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME);
935 enabledDeviceExtensions.push_back(
936 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME);
937 InitExternalMemoryHardwareBufferANDROIDFunctions(mInstance);
938 }
939#else
940 ASSERT(!getFeatures().supportsAndroidHardwareBuffer);
941#endif
942
Frank Henigman52047de2018-11-13 17:22:36 -0500943 std::sort(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(), StrLess);
944 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionNames, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500945
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400946 // Select additional features to be enabled
Frank Henigman52047de2018-11-13 17:22:36 -0500947 VkPhysicalDeviceFeatures2KHR enabledFeatures = {};
948 enabledFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
949 enabledFeatures.features.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
950 enabledFeatures.features.robustBufferAccess = mPhysicalDeviceFeatures.robustBufferAccess;
Shahbaz Youssefi962c2222019-02-20 15:43:41 -0500951 enabledFeatures.features.samplerAnisotropy = mPhysicalDeviceFeatures.samplerAnisotropy;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400952
Frank Henigman52047de2018-11-13 17:22:36 -0500953 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT divisorFeatures = {};
954 divisorFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT;
955 divisorFeatures.vertexAttributeInstanceRateDivisor = true;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500956
Shahbaz Youssefi76bd8482019-02-13 13:00:44 -0500957 float zeroPriority = 0.0f;
Frank Henigman52047de2018-11-13 17:22:36 -0500958 VkDeviceQueueCreateInfo queueCreateInfo = {};
Shahbaz Youssefi76bd8482019-02-13 13:00:44 -0500959 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
960 queueCreateInfo.flags = 0;
961 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
962 queueCreateInfo.queueCount = 1;
963 queueCreateInfo.pQueuePriorities = &zeroPriority;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500964
965 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400966 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500967
Shahbaz Youssefi76bd8482019-02-13 13:00:44 -0500968 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
969 createInfo.flags = 0;
970 createInfo.queueCreateInfoCount = 1;
971 createInfo.pQueueCreateInfos = &queueCreateInfo;
972 createInfo.enabledLayerCount = enabledDeviceLayerNames.size();
973 createInfo.ppEnabledLayerNames = enabledDeviceLayerNames.data();
Frank Henigman52047de2018-11-13 17:22:36 -0500974
975 if (vkGetPhysicalDeviceProperties2KHR &&
976 ExtensionFound(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, deviceExtensionNames))
977 {
978 enabledDeviceExtensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME);
979 enabledFeatures.pNext = &divisorFeatures;
980
981 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT divisorProperties = {};
982 divisorProperties.sType =
983 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT;
984
985 VkPhysicalDeviceProperties2 deviceProperties = {};
986 deviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
987 deviceProperties.pNext = &divisorProperties;
988
989 vkGetPhysicalDeviceProperties2KHR(mPhysicalDevice, &deviceProperties);
990 mMaxVertexAttribDivisor = divisorProperties.maxVertexAttribDivisor;
991
992 createInfo.pNext = &enabledFeatures;
993 }
994 else
995 {
996 createInfo.pEnabledFeatures = &enabledFeatures.features;
997 }
998
Jamie Madill4d0bf552016-12-28 15:45:24 -0500999 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
1000 createInfo.ppEnabledExtensionNames =
1001 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Jamie Madill4d0bf552016-12-28 15:45:24 -05001002
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001003 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001004
1005 mCurrentQueueFamilyIndex = queueFamilyIndex;
1006
1007 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
1008
1009 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001010 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -05001011 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1012 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
1013 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001014
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001015 ANGLE_VK_TRY(displayVk, mCommandPool.init(mDevice, commandPoolInfo));
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001016
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001017 // Initialize the vulkan pipeline cache.
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001018 ANGLE_TRY(initPipelineCache(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001019
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001020 // Initialize the submission semaphore pool.
1021 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
1022
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001023#if ANGLE_ENABLE_VULKAN_GPU_TRACE_EVENTS
1024 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1025 ASSERT(platform);
1026
1027 // GPU tracing workaround for anglebug.com/2927. The renderer should not emit gpu events during
1028 // platform discovery.
1029 const unsigned char *gpuEventsEnabled =
1030 platform->getTraceCategoryEnabledFlag(platform, "gpu.angle.gpu");
1031 mGpuEventsEnabled = gpuEventsEnabled && *gpuEventsEnabled;
1032#endif
1033
1034 if (mGpuEventsEnabled)
1035 {
1036 // Calculate the difference between CPU and GPU clocks for GPU event reporting.
1037 ANGLE_TRY(mGpuEventQueryPool.init(displayVk, VK_QUERY_TYPE_TIMESTAMP,
1038 vk::kDefaultTimestampQueryPoolSize));
1039 ANGLE_TRY(synchronizeCpuGpuTime(displayVk));
1040 }
1041
Jamie Madill7c985f52018-11-29 18:16:17 -05001042 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001043}
1044
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001045angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -04001046 VkSurfaceKHR surface,
1047 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001048{
1049 // We've already initialized a device, and can't re-create it unless it's never been used.
1050 // TODO(jmadill): Handle the re-creation case if necessary.
1051 if (mDevice != VK_NULL_HANDLE)
1052 {
1053 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
1054
1055 // Check if the current device supports present on this surface.
1056 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001057 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -04001058 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -05001059 surface, &supportsPresent));
1060
Jamie Madill6cad7732018-07-11 09:01:17 -04001061 if (supportsPresent == VK_TRUE)
1062 {
1063 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill7c985f52018-11-29 18:16:17 -05001064 return angle::Result::Continue;
Jamie Madill6cad7732018-07-11 09:01:17 -04001065 }
Jamie Madill4d0bf552016-12-28 15:45:24 -05001066 }
1067
1068 // Find a graphics and present queue.
1069 Optional<uint32_t> newPresentQueue;
1070 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
Shahbaz Youssefi823d8972018-11-13 10:52:40 -05001071 constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001072 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
1073 {
1074 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
Shahbaz Youssefi823d8972018-11-13 10:52:40 -05001075 if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001076 {
1077 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001078 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
1079 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001080
1081 if (supportsPresent == VK_TRUE)
1082 {
1083 newPresentQueue = queueIndex;
1084 break;
1085 }
1086 }
1087 }
1088
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001089 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
1090 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -05001091
Jamie Madill6cad7732018-07-11 09:01:17 -04001092 *presentQueueOut = newPresentQueue.value();
Jamie Madill7c985f52018-11-29 18:16:17 -05001093 return angle::Result::Continue;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001094}
1095
1096std::string RendererVk::getVendorString() const
1097{
Olli Etuahoc6a06182018-04-13 14:11:46 +03001098 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -05001099}
1100
Jamie Madille09bd5d2016-11-29 16:20:35 -05001101std::string RendererVk::getRendererDescription() const
1102{
Jamie Madill4d0bf552016-12-28 15:45:24 -05001103 std::stringstream strstr;
1104
1105 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
1106
1107 strstr << "Vulkan ";
1108 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
1109 strstr << VK_VERSION_MINOR(apiVersion) << ".";
1110 strstr << VK_VERSION_PATCH(apiVersion);
1111
Olli Etuahoc6a06182018-04-13 14:11:46 +03001112 strstr << "(";
1113
1114 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
1115 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
1116 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
1117 // driver detection.
1118 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
1119 {
1120 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
1121 }
1122
Geoff Langa7af56b2018-12-14 14:20:28 -05001123 strstr << mPhysicalDeviceProperties.deviceName;
1124 strstr << " (" << gl::FmtHex(mPhysicalDeviceProperties.deviceID) << ")";
1125
1126 strstr << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -05001127
1128 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -05001129}
1130
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001131gl::Version RendererVk::getMaxSupportedESVersion() const
1132{
Geoff Lang0c2c9232019-01-14 16:01:38 -05001133 // Current highest supported version
Shahbaz Youssefi9dc8eaf2019-02-11 11:04:54 -05001134 gl::Version maxVersion = gl::Version(3, 0);
1135
1136#if ANGLE_VULKAN_CONFORMANT_CONFIGS_ONLY
1137 // TODO: Disallow ES 3.0 until supported. http://crbug.com/angleproject/2950
1138 maxVersion = gl::Version(2, 0);
1139#endif
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001140
Geoff Lang0c2c9232019-01-14 16:01:38 -05001141 // Vulkan inherited queries are required to support any GL query type
1142 if (!mPhysicalDeviceFeatures.inheritedQueries)
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001143 {
Geoff Lang0c2c9232019-01-14 16:01:38 -05001144 maxVersion = std::max(maxVersion, gl::Version(2, 0));
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001145 }
1146
Geoff Lang0c2c9232019-01-14 16:01:38 -05001147 return maxVersion;
Shahbaz Youssefi092481a2018-11-08 00:25:50 -05001148}
1149
Frank Henigman52047de2018-11-13 17:22:36 -05001150void RendererVk::initFeatures(const ExtensionNameList &deviceExtensionNames)
Jamie Madill12222072018-07-11 14:59:48 -04001151{
Jamie Madillb36a4812018-09-25 10:15:11 -04001152// Use OpenGL line rasterization rules by default.
1153// TODO(jmadill): Fix Android support. http://anglebug.com/2830
1154#if defined(ANGLE_PLATFORM_ANDROID)
1155 mFeatures.basicGLLineRasterization = false;
1156#else
Jamie Madill12222072018-07-11 14:59:48 -04001157 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -04001158#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -04001159
Ian Elliott52f5da42018-12-21 09:02:09 -07001160 if ((mPhysicalDeviceProperties.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) ||
Frank Henigman52047de2018-11-13 17:22:36 -05001161 ExtensionFound(VK_KHR_MAINTENANCE1_EXTENSION_NAME, deviceExtensionNames))
Ian Elliottd50521f2018-12-20 12:05:14 -07001162 {
1163 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
1164 // investigation. http://anglebug.com/2728
1165 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
1166 }
Frank Henigmanbeb669d2018-09-21 16:25:52 -04001167
1168#ifdef ANGLE_PLATFORM_WINDOWS
1169 // http://anglebug.com/2838
1170 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
Shahbaz Youssefi4f3b2072019-01-01 14:48:25 -05001171
1172 // http://anglebug.com/3055
1173 mFeatures.forceCpuPathForCubeMapCopy = IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -04001174#endif
Shahbaz Youssefid856ca42018-10-31 16:55:12 -04001175
1176 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1177 platform->overrideFeaturesVk(platform, &mFeatures);
Jamie Madillfde74c02018-11-18 16:12:02 -05001178
1179 // Work around incorrect NVIDIA point size range clamping.
1180 // TODO(jmadill): Narrow driver range once fixed. http://anglebug.com/2970
1181 if (IsNvidia(mPhysicalDeviceProperties.vendorID))
1182 {
1183 mFeatures.clampPointSize = true;
1184 }
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001185
Jamie Madillfa7ca182019-01-15 11:20:58 -05001186 // We also need to clamp point size on several Android drivers.
1187 // TODO(jmadill): Remove suppression once fixed. http://anglebug.com/2599
1188 if (IsAndroid())
1189 {
1190 mFeatures.clampPointSize = true;
1191 }
1192
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001193#if defined(ANGLE_PLATFORM_ANDROID)
Shahbaz Youssefib08457d2018-12-11 15:13:54 -05001194 // Work around ineffective compute-graphics barriers on Nexus 5X.
1195 // TODO(syoussefi): Figure out which other vendors and driver versions are affected.
1196 // http://anglebug.com/3019
1197 mFeatures.flushAfterVertexConversion =
1198 IsNexus5X(mPhysicalDeviceProperties.vendorID, mPhysicalDeviceProperties.deviceID);
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001199#endif
Ian Elliottbcb78902018-12-19 11:46:29 -07001200
Frank Henigman52047de2018-11-13 17:22:36 -05001201 if (ExtensionFound(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, deviceExtensionNames))
Ian Elliottbcb78902018-12-19 11:46:29 -07001202 {
1203 mFeatures.supportsIncrementalPresent = true;
1204 }
Geoff Lang009696c2019-01-31 14:47:07 -05001205
1206#if defined(ANGLE_PLATFORM_ANDROID)
Yuly Novikovdbbdf562019-02-27 19:48:16 -05001207 mFeatures.supportsAndroidHardwareBuffer =
1208 ExtensionFound(VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
1209 deviceExtensionNames) &&
1210 ExtensionFound(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, deviceExtensionNames);
Geoff Lang009696c2019-01-31 14:47:07 -05001211#endif
Yuly Novikov1581ff02019-02-28 20:59:26 -05001212
1213 if (IsLinux() && IsIntel(mPhysicalDeviceProperties.vendorID))
1214 {
1215 mFeatures.disableFifoPresentMode = true;
1216 }
Jamie Madill12222072018-07-11 14:59:48 -04001217}
1218
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001219void RendererVk::initPipelineCacheVkKey()
1220{
1221 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
1222 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
1223 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
1224 // there is no '\0' in the result.
1225 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
1226 {
1227 hashStream << std::hex << c;
1228 }
1229 // Add the vendor and device id too for good measure.
1230 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
1231 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
1232
1233 const std::string &hashString = hashStream.str();
1234 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
1235 hashString.length(), mPipelineCacheVkBlobKey.data());
1236}
1237
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001238angle::Result RendererVk::initPipelineCache(DisplayVk *display)
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001239{
1240 initPipelineCacheVkKey();
1241
1242 egl::BlobCache::Value initialData;
1243 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
1244 mPipelineCacheVkBlobKey, &initialData);
1245
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001246 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001247
1248 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001249 pipelineCacheCreateInfo.flags = 0;
1250 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
1251 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
1252
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001253 ANGLE_VK_TRY(display, mPipelineCache.init(mDevice, pipelineCacheCreateInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -05001254 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001255}
1256
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001257void RendererVk::getSubmitWaitSemaphores(
1258 vk::Context *context,
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001259 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores,
1260 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> *waitStageMasks)
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001261{
1262 if (mSubmitLastSignaledSemaphore.getSemaphore())
1263 {
1264 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001265 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001266
1267 // Return the semaphore to the pool (which will remain valid and unused until the
1268 // queue it's about to be waited on has finished execution).
1269 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1270 }
1271
1272 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
1273 {
1274 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001275 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
1276
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001277 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
1278 }
1279 mSubmitWaitSemaphores.clear();
1280}
1281
Jamie Madillacccc6c2016-05-03 17:22:10 -04001282const gl::Caps &RendererVk::getNativeCaps() const
1283{
1284 ensureCapsInitialized();
1285 return mNativeCaps;
1286}
1287
1288const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
1289{
1290 ensureCapsInitialized();
1291 return mNativeTextureCaps;
1292}
1293
1294const gl::Extensions &RendererVk::getNativeExtensions() const
1295{
1296 ensureCapsInitialized();
1297 return mNativeExtensions;
1298}
1299
1300const gl::Limitations &RendererVk::getNativeLimitations() const
1301{
1302 ensureCapsInitialized();
1303 return mNativeLimitations;
1304}
1305
Luc Ferrondaedf4d2018-03-16 09:28:53 -04001306uint32_t RendererVk::getMaxActiveTextures()
1307{
1308 // TODO(lucferron): expose this limitation to GL in Context Caps
1309 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
1310 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
1311}
1312
Jamie Madill49ac74b2017-12-21 14:42:33 -05001313const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -05001314{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001315 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001316}
1317
Jamie Madill21061022018-07-12 23:56:30 -04001318angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -05001319{
Jamie Madill1f46bc12018-02-20 16:09:43 -05001320 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -05001321 {
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001322 TRACE_EVENT0("gpu.angle", "RendererVk::finish");
1323
Luc Ferron1617e692018-07-11 11:08:19 -04001324 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1325 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001326
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001327 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001328 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1329 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001330
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001331 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001332 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001333 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1334 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001335 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001336 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -04001337 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001338 submitInfo.signalSemaphoreCount = 0;
1339 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -05001340
Jamie Madill21061022018-07-12 23:56:30 -04001341 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001342 }
Jamie Madill4d0bf552016-12-28 15:45:24 -05001343
Jamie Madill4c26fc22017-02-24 11:04:10 -05001344 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -04001345 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001346 freeAllInFlightResources();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001347
1348 if (mGpuEventsEnabled)
1349 {
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001350 // This loop should in practice execute once since the queue is already idle.
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001351 while (mInFlightGpuEventQueries.size() > 0)
1352 {
1353 ANGLE_TRY(checkCompletedGpuEvents(context));
1354 }
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001355 // Recalculate the CPU/GPU time difference to account for clock drifting. Avoid unnecessary
1356 // synchronization if there is no event to be adjusted (happens when finish() gets called
1357 // multiple times towards the end of the application).
1358 if (mGpuEvents.size() > 0)
1359 {
1360 ANGLE_TRY(synchronizeCpuGpuTime(context));
1361 }
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001362 }
1363
Jamie Madill7c985f52018-11-29 18:16:17 -05001364 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001365}
1366
Jamie Madill0c0dc342017-03-24 14:18:51 -04001367void RendererVk::freeAllInFlightResources()
1368{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001369 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -04001370 {
Yuly Novikovb56ddbb2018-11-02 16:53:18 -04001371 // On device loss we need to wait for fence to be signaled before destroying it
1372 if (mDeviceLost)
1373 {
1374 VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs);
1375 // If wait times out, it is probably not possible to recover from lost device
1376 ASSERT(status == VK_SUCCESS || status == VK_ERROR_DEVICE_LOST);
1377 }
Jamie Madill49ac74b2017-12-21 14:42:33 -05001378 batch.fence.destroy(mDevice);
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001379 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001380 }
1381 mInFlightCommands.clear();
1382
1383 for (auto &garbage : mGarbage)
1384 {
Jamie Madille88ec8e2017-10-31 17:18:14 -04001385 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001386 }
1387 mGarbage.clear();
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001388
1389 mLastCompletedQueueSerial = mLastSubmittedQueueSerial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001390}
1391
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001392angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001393{
Jamie Madill49ac74b2017-12-21 14:42:33 -05001394 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -05001395
Jamie Madill49ac74b2017-12-21 14:42:33 -05001396 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001397 {
Yuly Novikov27780292018-11-09 11:19:49 -05001398 VkResult result = batch.fence.getStatus(mDevice);
1399 if (result == VK_NOT_READY)
1400 {
Jamie Madill0c0dc342017-03-24 14:18:51 -04001401 break;
Yuly Novikov27780292018-11-09 11:19:49 -05001402 }
1403 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001404
Jamie Madill49ac74b2017-12-21 14:42:33 -05001405 ASSERT(batch.serial > mLastCompletedQueueSerial);
1406 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -04001407
Jamie Madill49ac74b2017-12-21 14:42:33 -05001408 batch.fence.destroy(mDevice);
Tobin Ehlis4a419142019-02-05 08:50:30 -07001409 TRACE_EVENT0("gpu.angle", "commandPool.destroy");
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001410 batch.commandPool.destroy(mDevice);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001411 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001412 }
1413
Jamie Madill49ac74b2017-12-21 14:42:33 -05001414 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001415
1416 size_t freeIndex = 0;
1417 for (; freeIndex < mGarbage.size(); ++freeIndex)
1418 {
Jamie Madill49ac74b2017-12-21 14:42:33 -05001419 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -04001420 break;
1421 }
1422
1423 // Remove the entries from the garbage list - they should be ready to go.
1424 if (freeIndex > 0)
1425 {
1426 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -05001427 }
1428
Jamie Madill7c985f52018-11-29 18:16:17 -05001429 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001430}
1431
Jamie Madill21061022018-07-12 23:56:30 -04001432angle::Result RendererVk::submitFrame(vk::Context *context,
1433 const VkSubmitInfo &submitInfo,
1434 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -05001435{
Tobin Ehlis573f76b2018-05-03 11:10:44 -06001436 TRACE_EVENT0("gpu.angle", "RendererVk::submitFrame");
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001437 VkFenceCreateInfo fenceInfo = {};
Jamie Madillb980c562018-11-27 11:34:27 -05001438 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1439 fenceInfo.flags = 0;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001440
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001441 vk::Scoped<CommandBatch> scopedBatch(mDevice);
Jamie Madillbea35a62018-07-05 11:54:10 -04001442 CommandBatch &batch = scopedBatch.get();
Yuly Novikov27780292018-11-09 11:19:49 -05001443 ANGLE_VK_TRY(context, batch.fence.init(mDevice, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001444
Jamie Madill21061022018-07-12 23:56:30 -04001445 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -05001446
1447 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -05001448 batch.commandPool = std::move(mCommandPool);
1449 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001450
Jamie Madillbea35a62018-07-05 11:54:10 -04001451 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -04001452
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001453 // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on
1454 // swap() though, and there could be multiple submissions in between (through glFlush() calls),
Shahbaz Youssefi611bbaa2018-12-06 01:59:53 +01001455 // so the limit is larger than the expected number of images. The
1456 // InterleavedAttributeDataBenchmark perf test for example issues a large number of flushes.
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001457 ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit);
Jamie Madill0c0dc342017-03-24 14:18:51 -04001458
Jamie Madill85ca1892019-01-16 13:27:15 -05001459 nextSerial();
Jamie Madill0c0dc342017-03-24 14:18:51 -04001460
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001461 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001462
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001463 if (mGpuEventsEnabled)
1464 {
1465 ANGLE_TRY(checkCompletedGpuEvents(context));
1466 }
1467
Jamie Madill49ac74b2017-12-21 14:42:33 -05001468 // Simply null out the command buffer here - it was allocated using the command pool.
1469 commandBuffer.releaseHandle();
1470
1471 // Reallocate the command pool for next frame.
1472 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001473 VkCommandPoolCreateInfo poolInfo = {};
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001474 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001475 poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001476 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001477
Tobin Ehlis47ca1b22019-01-23 16:11:41 +00001478 ANGLE_VK_TRY(context, mCommandPool.init(mDevice, poolInfo));
Jamie Madill7c985f52018-11-29 18:16:17 -05001479 return angle::Result::Continue;
Jamie Madill4c26fc22017-02-24 11:04:10 -05001480}
1481
Jamie Madill85ca1892019-01-16 13:27:15 -05001482void RendererVk::nextSerial()
1483{
1484 // Increment the queue serial. If this fails, we should restart ANGLE.
1485 mLastSubmittedQueueSerial = mCurrentQueueSerial;
1486 mCurrentQueueSerial = mQueueSerialFactory.generate();
1487
1488 // Notify the Contexts that they should be starting new command buffers.
1489 // We use one command pool per serial/submit associated with this VkQueue. We can also
1490 // have multiple Contexts sharing one VkQueue. In ContextVk::setupDraw we don't explicitly
1491 // check for a new serial when starting a new command buffer. We just check that the current
1492 // recording command buffer is valid. Thus we need to explicitly notify every other Context
1493 // using this VkQueue that they their current command buffer is no longer valid.
1494 for (gl::Context *context : mDisplay->getContextSet())
1495 {
1496 ContextVk *contextVk = vk::GetImpl(context);
1497 contextVk->onCommandBufferFinished();
1498 }
1499}
1500
Jamie Madillaaca96e2018-06-12 10:19:48 -04001501bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -05001502{
1503 return serial > mLastCompletedQueueSerial;
1504}
1505
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001506angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
1507{
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001508 bool timedOut = false;
1509 angle::Result result = finishToSerialOrTimeout(context, serial, kMaxFenceWaitTimeNs, &timedOut);
1510
1511 // Don't tolerate timeout. If such a large wait time results in timeout, something's wrong.
1512 if (timedOut)
1513 {
1514 result = angle::Result::Stop;
1515 }
1516 return result;
1517}
1518
1519angle::Result RendererVk::finishToSerialOrTimeout(vk::Context *context,
1520 Serial serial,
1521 uint64_t timeout,
1522 bool *outTimedOut)
1523{
1524 *outTimedOut = false;
1525
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001526 if (!isSerialInUse(serial) || mInFlightCommands.empty())
1527 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001528 return angle::Result::Continue;
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001529 }
1530
1531 // Find the first batch with serial equal to or bigger than given serial (note that
1532 // the batch serials are unique, otherwise upper-bound would have been necessary).
1533 size_t batchIndex = mInFlightCommands.size() - 1;
1534 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
1535 {
1536 if (mInFlightCommands[i].serial >= serial)
1537 {
1538 batchIndex = i;
1539 break;
1540 }
1541 }
1542 const CommandBatch &batch = mInFlightCommands[batchIndex];
1543
1544 // Wait for it finish
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001545 VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs);
1546
1547 // If timed out, report it as such.
1548 if (status == VK_TIMEOUT)
1549 {
1550 *outTimedOut = true;
1551 return angle::Result::Continue;
1552 }
1553
1554 ANGLE_VK_TRY(context, status);
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001555
1556 // Clean up finished batches.
1557 return checkCompletedCommands(context);
1558}
1559
Jamie Madill21061022018-07-12 23:56:30 -04001560angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1561 const vk::RenderPassDesc &desc,
1562 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001563{
Jamie Madill21061022018-07-12 23:56:30 -04001564 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001565 renderPassOut);
1566}
1567
Jamie Madill21061022018-07-12 23:56:30 -04001568angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1569 const vk::RenderPassDesc &desc,
1570 const vk::AttachmentOpsArray &ops,
1571 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001572{
Jamie Madill21061022018-07-12 23:56:30 -04001573 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001574 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001575}
1576
Jamie Madilla5e06072018-05-18 14:36:05 -04001577vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001578{
Jamie Madilla5e06072018-05-18 14:36:05 -04001579 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001580}
1581
Jamie Madill21061022018-07-12 23:56:30 -04001582angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001583{
Jamie Madill21061022018-07-12 23:56:30 -04001584 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001585 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001586}
1587
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001588angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001589{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001590 if (mCommandGraph.empty())
1591 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001592 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001593 }
1594
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001595 TRACE_EVENT0("gpu.angle", "RendererVk::flush");
1596
Jamie Madillbea35a62018-07-05 11:54:10 -04001597 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1598 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001599
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001600 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001601 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1602 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001603
1604 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1605 // will be waited on.
1606 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001607
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001608 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001609 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001610 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1611 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001612 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001613 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001614 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001615 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001616 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001617
Jamie Madill21061022018-07-12 23:56:30 -04001618 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001619
Jamie Madill7c985f52018-11-29 18:16:17 -05001620 return angle::Result::Continue;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001621}
1622
Jamie Madill78feddc2018-04-27 11:45:05 -04001623Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001624{
Jamie Madill78feddc2018-04-27 11:45:05 -04001625 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001626}
1627
Jamie Madill21061022018-07-12 23:56:30 -04001628angle::Result RendererVk::getDescriptorSetLayout(
1629 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001630 const vk::DescriptorSetLayoutDesc &desc,
1631 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1632{
Jamie Madill21061022018-07-12 23:56:30 -04001633 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001634}
1635
Jamie Madill21061022018-07-12 23:56:30 -04001636angle::Result RendererVk::getPipelineLayout(
1637 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001638 const vk::PipelineLayoutDesc &desc,
1639 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1640 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1641{
Jamie Madill21061022018-07-12 23:56:30 -04001642 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001643 pipelineLayoutOut);
1644}
1645
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001646angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1647{
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001648 ASSERT(mPipelineCache.valid());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001649
1650 if (--mPipelineCacheVkUpdateTimeout > 0)
1651 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001652 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001653 }
1654
1655 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1656
1657 // Get the size of the cache.
1658 size_t pipelineCacheSize = 0;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001659 VkResult result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, nullptr);
Yuly Novikov27780292018-11-09 11:19:49 -05001660 if (result != VK_INCOMPLETE)
1661 {
1662 ANGLE_VK_TRY(displayVk, result);
1663 }
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001664
1665 angle::MemoryBuffer *pipelineCacheData = nullptr;
1666 ANGLE_VK_CHECK_ALLOC(displayVk,
1667 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1668
1669 size_t originalPipelineCacheSize = pipelineCacheSize;
Jamie Madilldc65c5b2018-11-21 11:07:26 -05001670 result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, pipelineCacheData->data());
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001671 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1672 // was determined just above), so receiving it hints at an implementation bug we would want
1673 // to know about early.
Yuly Novikov27780292018-11-09 11:19:49 -05001674 ASSERT(result != VK_INCOMPLETE);
1675 ANGLE_VK_TRY(displayVk, result);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001676
1677 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1678 // the buffer to avoid leaking garbage memory.
1679 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1680 if (pipelineCacheSize < originalPipelineCacheSize)
1681 {
1682 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1683 originalPipelineCacheSize - pipelineCacheSize);
1684 }
1685
1686 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1687
Jamie Madill7c985f52018-11-29 18:16:17 -05001688 return angle::Result::Continue;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001689}
1690
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001691angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1692 const vk::Semaphore **outSemaphore)
1693{
1694 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1695
1696 vk::SemaphoreHelper semaphore;
1697 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1698
1699 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1700 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1701
Jamie Madill7c985f52018-11-29 18:16:17 -05001702 return angle::Result::Continue;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001703}
1704
1705const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1706{
1707 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1708
1709 // Return the semaphore to the pool (which will remain valid and unused until the
1710 // queue it's about to be waited on has finished execution). The caller is about
1711 // to wait on it.
1712 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1713
1714 return semaphore;
1715}
1716
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001717angle::Result RendererVk::getTimestamp(vk::Context *context, uint64_t *timestampOut)
1718{
1719 // The intent of this function is to query the timestamp without stalling the GPU. Currently,
1720 // that seems impossible, so instead, we are going to make a small submission with just a
1721 // timestamp query. First, the disjoint timer query extension says:
1722 //
1723 // > This will return the GL time after all previous commands have reached the GL server but
1724 // have not yet necessarily executed.
1725 //
1726 // The previous commands are stored in the command graph at the moment and are not yet flushed.
1727 // The wording allows us to make a submission to get the timestamp without performing a flush.
1728 //
1729 // Second:
1730 //
1731 // > By using a combination of this synchronous get command and the asynchronous timestamp query
1732 // object target, applications can measure the latency between when commands reach the GL server
1733 // and when they are realized in the framebuffer.
1734 //
1735 // This fits with the above strategy as well, although inevitably we are possibly introducing a
1736 // GPU bubble. This function directly generates a command buffer and submits it instead of
1737 // using the other member functions. This is to avoid changing any state, such as the queue
1738 // serial.
1739
1740 // Create a query used to receive the GPU timestamp
1741 vk::Scoped<vk::DynamicQueryPool> timestampQueryPool(mDevice);
1742 vk::QueryHelper timestampQuery;
1743 ANGLE_TRY(timestampQueryPool.get().init(context, VK_QUERY_TYPE_TIMESTAMP, 1));
1744 ANGLE_TRY(timestampQueryPool.get().allocateQuery(context, &timestampQuery));
1745
1746 // Record the command buffer
1747 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1748 vk::CommandBuffer &commandBuffer = commandBatch.get();
1749
1750 VkCommandBufferAllocateInfo commandBufferInfo = {};
1751 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1752 commandBufferInfo.commandPool = mCommandPool.getHandle();
1753 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1754 commandBufferInfo.commandBufferCount = 1;
1755
Yuly Novikov27780292018-11-09 11:19:49 -05001756 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001757
1758 VkCommandBufferBeginInfo beginInfo = {};
1759 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1760 beginInfo.flags = 0;
1761 beginInfo.pInheritanceInfo = nullptr;
1762
Yuly Novikov27780292018-11-09 11:19:49 -05001763 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001764
1765 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1766 timestampQuery.getQuery(), 1);
1767 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1768 timestampQuery.getQueryPool()->getHandle(),
1769 timestampQuery.getQuery());
1770
Yuly Novikov27780292018-11-09 11:19:49 -05001771 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001772
1773 // Create fence for the submission
1774 VkFenceCreateInfo fenceInfo = {};
1775 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1776 fenceInfo.flags = 0;
1777
1778 vk::Scoped<vk::Fence> fence(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001779 ANGLE_VK_TRY(context, fence.get().init(mDevice, fenceInfo));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001780
1781 // Submit the command buffer
1782 VkSubmitInfo submitInfo = {};
1783 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1784 submitInfo.waitSemaphoreCount = 0;
1785 submitInfo.pWaitSemaphores = nullptr;
1786 submitInfo.pWaitDstStageMask = nullptr;
1787 submitInfo.commandBufferCount = 1;
1788 submitInfo.pCommandBuffers = commandBuffer.ptr();
1789 submitInfo.signalSemaphoreCount = 0;
1790 submitInfo.pSignalSemaphores = nullptr;
1791
1792 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, fence.get().getHandle()));
1793
1794 // Wait for the submission to finish. Given no semaphores, there is hope that it would execute
1795 // in parallel with what's already running on the GPU.
Yuly Novikov27780292018-11-09 11:19:49 -05001796 ANGLE_VK_TRY(context, fence.get().wait(mDevice, kMaxFenceWaitTimeNs));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001797
1798 // Get the query results
1799 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1800
Yuly Novikov27780292018-11-09 11:19:49 -05001801 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
1802 mDevice, timestampQuery.getQuery(), 1, sizeof(*timestampOut),
1803 timestampOut, sizeof(*timestampOut), queryFlags));
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001804
1805 timestampQueryPool.get().freeQuery(context, &timestampQuery);
1806
Shahbaz Youssefi5904ee32019-01-25 11:15:16 -05001807 // Convert results to nanoseconds.
1808 *timestampOut = static_cast<uint64_t>(
1809 *timestampOut * static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod));
1810
Jamie Madill7c985f52018-11-29 18:16:17 -05001811 return angle::Result::Continue;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001812}
1813
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05001814// These functions look at the mandatory format for support, and fallback to querying the device (if
1815// necessary) to test the availability of the bits.
1816bool RendererVk::hasLinearTextureFormatFeatureBits(VkFormat format,
1817 const VkFormatFeatureFlags featureBits)
1818{
1819 return hasFormatFeatureBits<&VkFormatProperties::linearTilingFeatures>(format, featureBits);
1820}
1821
1822bool RendererVk::hasTextureFormatFeatureBits(VkFormat format,
1823 const VkFormatFeatureFlags featureBits)
1824{
1825 return hasFormatFeatureBits<&VkFormatProperties::optimalTilingFeatures>(format, featureBits);
1826}
1827
1828bool RendererVk::hasBufferFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
1829{
1830 return hasFormatFeatureBits<&VkFormatProperties::bufferFeatures>(format, featureBits);
1831}
1832
Shahbaz Youssefi4d153382019-02-26 15:08:11 +00001833void RendererVk::insertDebugMarker(GLenum source, GLuint id, std::string &&marker)
1834{
1835 mCommandGraph.insertDebugMarker(source, std::move(marker));
1836}
1837
1838void RendererVk::pushDebugMarker(GLenum source, GLuint id, std::string &&marker)
1839{
1840 mCommandGraph.pushDebugMarker(source, std::move(marker));
1841}
1842
1843void RendererVk::popDebugMarker()
1844{
1845 mCommandGraph.popDebugMarker();
1846}
1847
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001848angle::Result RendererVk::synchronizeCpuGpuTime(vk::Context *context)
1849{
1850 ASSERT(mGpuEventsEnabled);
1851
1852 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1853 ASSERT(platform);
1854
1855 // To synchronize CPU and GPU times, we need to get the CPU timestamp as close as possible to
1856 // the GPU timestamp. The process of getting the GPU timestamp is as follows:
1857 //
1858 // CPU GPU
1859 //
1860 // Record command buffer
1861 // with timestamp query
1862 //
1863 // Submit command buffer
1864 //
1865 // Post-submission work Begin execution
1866 //
Tobin Ehlis5546fb42019-01-17 12:25:54 -05001867 // ???? Write timestamp Tgpu
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001868 //
1869 // ???? End execution
1870 //
1871 // ???? Return query results
1872 //
1873 // ????
1874 //
1875 // Get query results
1876 //
1877 // The areas of unknown work (????) on the CPU indicate that the CPU may or may not have
1878 // finished post-submission work while the GPU is executing in parallel. With no further work,
1879 // querying CPU timestamps before submission and after getting query results give the bounds to
1880 // Tgpu, which could be quite large.
1881 //
1882 // Using VkEvents, the GPU can be made to wait for the CPU and vice versa, in an effort to
1883 // reduce this range. This function implements the following procedure:
1884 //
1885 // CPU GPU
1886 //
1887 // Record command buffer
1888 // with timestamp query
1889 //
1890 // Submit command buffer
1891 //
1892 // Post-submission work Begin execution
1893 //
1894 // ???? Set Event GPUReady
1895 //
1896 // Wait on Event GPUReady Wait on Event CPUReady
1897 //
1898 // Get CPU Time Ts Wait on Event CPUReady
1899 //
1900 // Set Event CPUReady Wait on Event CPUReady
1901 //
1902 // Get CPU Time Tcpu Get GPU Time Tgpu
1903 //
1904 // Wait on Event GPUDone Set Event GPUDone
1905 //
1906 // Get CPU Time Te End Execution
1907 //
1908 // Idle Return query results
1909 //
1910 // Get query results
1911 //
1912 // If Te-Ts > epsilon, a GPU or CPU interruption can be assumed and the operation can be
1913 // retried. Once Te-Ts < epsilon, Tcpu can be taken to presumably match Tgpu. Finding an
1914 // epsilon that's valid for all devices may be difficult, so the loop can be performed only a
1915 // limited number of times and the Tcpu,Tgpu pair corresponding to smallest Te-Ts used for
1916 // calibration.
1917 //
1918 // Note: Once VK_EXT_calibrated_timestamps is ubiquitous, this should be redone.
1919
1920 // Make sure nothing is running
1921 ASSERT(mCommandGraph.empty());
1922
1923 TRACE_EVENT0("gpu.angle", "RendererVk::synchronizeCpuGpuTime");
1924
1925 // Create a query used to receive the GPU timestamp
1926 vk::QueryHelper timestampQuery;
1927 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &timestampQuery));
1928
1929 // Create the three events
1930 VkEventCreateInfo eventCreateInfo = {};
1931 eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
1932 eventCreateInfo.flags = 0;
1933
1934 vk::Scoped<vk::Event> cpuReady(mDevice), gpuReady(mDevice), gpuDone(mDevice);
Yuly Novikov27780292018-11-09 11:19:49 -05001935 ANGLE_VK_TRY(context, cpuReady.get().init(mDevice, eventCreateInfo));
1936 ANGLE_VK_TRY(context, gpuReady.get().init(mDevice, eventCreateInfo));
1937 ANGLE_VK_TRY(context, gpuDone.get().init(mDevice, eventCreateInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001938
1939 constexpr uint32_t kRetries = 10;
1940
1941 // Time suffixes used are S for seconds and Cycles for cycles
1942 double tightestRangeS = 1e6f;
1943 double TcpuS = 0;
1944 uint64_t TgpuCycles = 0;
1945 for (uint32_t i = 0; i < kRetries; ++i)
1946 {
1947 // Reset the events
Yuly Novikov27780292018-11-09 11:19:49 -05001948 ANGLE_VK_TRY(context, cpuReady.get().reset(mDevice));
1949 ANGLE_VK_TRY(context, gpuReady.get().reset(mDevice));
1950 ANGLE_VK_TRY(context, gpuDone.get().reset(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001951
1952 // Record the command buffer
1953 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1954 vk::CommandBuffer &commandBuffer = commandBatch.get();
1955
1956 VkCommandBufferAllocateInfo commandBufferInfo = {};
1957 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1958 commandBufferInfo.commandPool = mCommandPool.getHandle();
1959 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1960 commandBufferInfo.commandBufferCount = 1;
1961
Yuly Novikov27780292018-11-09 11:19:49 -05001962 ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001963
1964 VkCommandBufferBeginInfo beginInfo = {};
1965 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1966 beginInfo.flags = 0;
1967 beginInfo.pInheritanceInfo = nullptr;
1968
Yuly Novikov27780292018-11-09 11:19:49 -05001969 ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001970
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001971 commandBuffer.setEvent(gpuReady.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001972 commandBuffer.waitEvents(1, cpuReady.get().ptr(), VK_PIPELINE_STAGE_HOST_BIT,
1973 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, nullptr, 0, nullptr, 0,
1974 nullptr);
1975
1976 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1977 timestampQuery.getQuery(), 1);
1978 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1979 timestampQuery.getQueryPool()->getHandle(),
1980 timestampQuery.getQuery());
1981
Shahbaz Youssefi82fddcb2019-01-18 14:27:43 -05001982 commandBuffer.setEvent(gpuDone.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001983
Yuly Novikov27780292018-11-09 11:19:49 -05001984 ANGLE_VK_TRY(context, commandBuffer.end());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001985
1986 // Submit the command buffer
1987 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1988 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1989 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
1990
1991 VkSubmitInfo submitInfo = {};
1992 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1993 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1994 submitInfo.pWaitSemaphores = waitSemaphores.data();
1995 submitInfo.pWaitDstStageMask = waitStageMasks.data();
1996 submitInfo.commandBufferCount = 1;
1997 submitInfo.pCommandBuffers = commandBuffer.ptr();
1998 submitInfo.signalSemaphoreCount = 0;
1999 submitInfo.pSignalSemaphores = nullptr;
2000
2001 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBuffer)));
2002
2003 // Wait for GPU to be ready. This is a short busy wait.
Yuly Novikov27780292018-11-09 11:19:49 -05002004 VkResult result = VK_EVENT_RESET;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002005 do
2006 {
Yuly Novikov27780292018-11-09 11:19:49 -05002007 result = gpuReady.get().getStatus(mDevice);
2008 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
2009 {
2010 ANGLE_VK_TRY(context, result);
2011 }
2012 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002013
2014 double TsS = platform->monotonicallyIncreasingTime(platform);
2015
2016 // Tell the GPU to go ahead with the timestamp query.
Yuly Novikov27780292018-11-09 11:19:49 -05002017 ANGLE_VK_TRY(context, cpuReady.get().set(mDevice));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002018 double cpuTimestampS = platform->monotonicallyIncreasingTime(platform);
2019
2020 // Wait for GPU to be done. Another short busy wait.
2021 do
2022 {
Yuly Novikov27780292018-11-09 11:19:49 -05002023 result = gpuDone.get().getStatus(mDevice);
2024 if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
2025 {
2026 ANGLE_VK_TRY(context, result);
2027 }
2028 } while (result == VK_EVENT_RESET);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002029
2030 double TeS = platform->monotonicallyIncreasingTime(platform);
2031
2032 // Get the query results
2033 ANGLE_TRY(finishToSerial(context, getLastSubmittedQueueSerial()));
2034
2035 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
2036
2037 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05002038 ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults(
2039 mDevice, timestampQuery.getQuery(), 1, sizeof(gpuTimestampCycles),
2040 &gpuTimestampCycles, sizeof(gpuTimestampCycles), queryFlags));
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002041
2042 // Use the first timestamp queried as origin.
2043 if (mGpuEventTimestampOrigin == 0)
2044 {
2045 mGpuEventTimestampOrigin = gpuTimestampCycles;
2046 }
2047
2048 // Take these CPU and GPU timestamps if there is better confidence.
2049 double confidenceRangeS = TeS - TsS;
2050 if (confidenceRangeS < tightestRangeS)
2051 {
2052 tightestRangeS = confidenceRangeS;
2053 TcpuS = cpuTimestampS;
2054 TgpuCycles = gpuTimestampCycles;
2055 }
2056 }
2057
2058 mGpuEventQueryPool.freeQuery(context, &timestampQuery);
2059
2060 // timestampPeriod gives nanoseconds/cycle.
2061 double TgpuS = (TgpuCycles - mGpuEventTimestampOrigin) *
2062 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) /
2063 1'000'000'000.0;
2064
2065 flushGpuEvents(TgpuS, TcpuS);
2066
2067 mGpuClockSync.gpuTimestampS = TgpuS;
2068 mGpuClockSync.cpuTimestampS = TcpuS;
2069
Jamie Madill7c985f52018-11-29 18:16:17 -05002070 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002071}
2072
2073angle::Result RendererVk::traceGpuEventImpl(vk::Context *context,
2074 vk::CommandBuffer *commandBuffer,
2075 char phase,
2076 const char *name)
2077{
2078 ASSERT(mGpuEventsEnabled);
2079
2080 GpuEventQuery event;
2081
2082 event.name = name;
2083 event.phase = phase;
2084 event.serial = mCurrentQueueSerial;
2085
2086 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &event.queryPoolIndex, &event.queryIndex));
2087
2088 commandBuffer->resetQueryPool(
2089 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex, 1);
2090 commandBuffer->writeTimestamp(
2091 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
2092 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex);
2093
2094 mInFlightGpuEventQueries.push_back(std::move(event));
2095
Jamie Madill7c985f52018-11-29 18:16:17 -05002096 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002097}
2098
2099angle::Result RendererVk::checkCompletedGpuEvents(vk::Context *context)
2100{
2101 ASSERT(mGpuEventsEnabled);
2102
2103 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
2104 ASSERT(platform);
2105
2106 int finishedCount = 0;
2107
2108 for (GpuEventQuery &eventQuery : mInFlightGpuEventQueries)
2109 {
2110 // Only check the timestamp query if the submission has finished.
2111 if (eventQuery.serial > mLastCompletedQueueSerial)
2112 {
2113 break;
2114 }
2115
2116 // See if the results are available.
2117 uint64_t gpuTimestampCycles = 0;
Yuly Novikov27780292018-11-09 11:19:49 -05002118 VkResult result = mGpuEventQueryPool.getQueryPool(eventQuery.queryPoolIndex)
2119 ->getResults(mDevice, eventQuery.queryIndex, 1,
2120 sizeof(gpuTimestampCycles), &gpuTimestampCycles,
2121 sizeof(gpuTimestampCycles), VK_QUERY_RESULT_64_BIT);
2122 if (result == VK_NOT_READY)
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002123 {
2124 break;
2125 }
Yuly Novikov27780292018-11-09 11:19:49 -05002126 ANGLE_VK_TRY(context, result);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002127
2128 mGpuEventQueryPool.freeQuery(context, eventQuery.queryPoolIndex, eventQuery.queryIndex);
2129
2130 GpuEvent event;
2131 event.gpuTimestampCycles = gpuTimestampCycles;
2132 event.name = eventQuery.name;
2133 event.phase = eventQuery.phase;
2134
2135 mGpuEvents.emplace_back(event);
2136
2137 ++finishedCount;
2138 }
2139
2140 mInFlightGpuEventQueries.erase(mInFlightGpuEventQueries.begin(),
2141 mInFlightGpuEventQueries.begin() + finishedCount);
2142
Jamie Madill7c985f52018-11-29 18:16:17 -05002143 return angle::Result::Continue;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04002144}
2145
2146void RendererVk::flushGpuEvents(double nextSyncGpuTimestampS, double nextSyncCpuTimestampS)
2147{
2148 if (mGpuEvents.size() == 0)
2149 {
2150 return;
2151 }
2152
2153 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
2154 ASSERT(platform);
2155
2156 // Find the slope of the clock drift for adjustment
2157 double lastGpuSyncTimeS = mGpuClockSync.gpuTimestampS;
2158 double lastGpuSyncDiffS = mGpuClockSync.cpuTimestampS - mGpuClockSync.gpuTimestampS;
2159 double gpuSyncDriftSlope = 0;
2160
2161 double nextGpuSyncTimeS = nextSyncGpuTimestampS;
2162 double nextGpuSyncDiffS = nextSyncCpuTimestampS - nextSyncGpuTimestampS;
2163
2164 // No gpu trace events should have been generated before the clock sync, so if there is no
2165 // "previous" clock sync, there should be no gpu events (i.e. the function early-outs above).
2166 ASSERT(mGpuClockSync.gpuTimestampS != std::numeric_limits<double>::max() &&
2167 mGpuClockSync.cpuTimestampS != std::numeric_limits<double>::max());
2168
2169 gpuSyncDriftSlope =
2170 (nextGpuSyncDiffS - lastGpuSyncDiffS) / (nextGpuSyncTimeS - lastGpuSyncTimeS);
2171
2172 for (const GpuEvent &event : mGpuEvents)
2173 {
2174 double gpuTimestampS =
2175 (event.gpuTimestampCycles - mGpuEventTimestampOrigin) *
2176 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) * 1e-9;
2177
2178 // Account for clock drift.
2179 gpuTimestampS += lastGpuSyncDiffS + gpuSyncDriftSlope * (gpuTimestampS - lastGpuSyncTimeS);
2180
2181 // Generate the trace now that the GPU timestamp is available and clock drifts are accounted
2182 // for.
2183 static long long eventId = 1;
2184 static const unsigned char *categoryEnabled =
2185 TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu.angle.gpu");
2186 platform->addTraceEvent(platform, event.phase, categoryEnabled, event.name, eventId++,
2187 gpuTimestampS, 0, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
2188 }
2189
2190 mGpuEvents.clear();
2191}
2192
Shahbaz Youssefi96bd8fd2018-11-30 14:30:18 -05002193template <VkFormatFeatureFlags VkFormatProperties::*features>
2194bool RendererVk::hasFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits)
2195{
2196 ASSERT(static_cast<uint32_t>(format) < vk::kNumVkFormats);
2197 VkFormatProperties &deviceProperties = mFormatProperties[format];
2198
2199 if (deviceProperties.bufferFeatures == kInvalidFormatFeatureFlags)
2200 {
2201 // If we don't have the actual device features, see if the requested features are mandatory.
2202 // If so, there's no need to query the device.
2203 const VkFormatProperties &mandatoryProperties = vk::GetMandatoryFormatSupport(format);
2204 if (IsMaskFlagSet(mandatoryProperties.*features, featureBits))
2205 {
2206 return true;
2207 }
2208
2209 // Otherwise query the format features and cache it.
2210 vkGetPhysicalDeviceFormatProperties(mPhysicalDevice, format, &deviceProperties);
2211 }
2212
2213 return IsMaskFlagSet(deviceProperties.*features, featureBits);
2214}
2215
Jamie Madillaaca96e2018-06-12 10:19:48 -04002216uint32_t GetUniformBufferDescriptorCount()
2217{
2218 return kUniformBufferDescriptorsPerDescriptorSet;
2219}
2220
Jamie Madill9e54b5a2016-05-25 12:57:39 -04002221} // namespace rx