blob: e1c8809bd07cd945e41e1c1d3673a4f38861e1ae [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
2// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// RendererVk.cpp:
7// Implements the class methods for RendererVk.
8//
9
10#include "libANGLE/renderer/vulkan/RendererVk.h"
11
Jamie Madill4d0bf552016-12-28 15:45:24 -050012// Placing this first seems to solve an intellisense bug.
Jamie Madill3c424b42018-01-19 12:35:09 -050013#include "libANGLE/renderer/vulkan/vk_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050014
Jamie Madille09bd5d2016-11-29 16:20:35 -050015#include <EGL/eglext.h>
16
Jamie Madill9e54b5a2016-05-25 12:57:39 -040017#include "common/debug.h"
Jamie Madilla66779f2017-01-06 10:43:44 -050018#include "common/system_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050019#include "libANGLE/renderer/driver_utils.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050020#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050021#include "libANGLE/renderer/vulkan/CompilerVk.h"
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040022#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050023#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050024#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050025#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050026#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050027#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050028#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050029#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040030
Shahbaz Youssefi61656022018-10-24 15:00:50 -040031#include "third_party/trace_event/trace_event.h"
32
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070033// Consts
34namespace
35{
36const uint32_t kMockVendorID = 0xba5eba11;
37const uint32_t kMockDeviceID = 0xf005ba11;
38constexpr char kMockDeviceName[] = "Vulkan Mock Device";
Shahbaz Youssefi61656022018-10-24 15:00:50 -040039constexpr size_t kInFlightCommandsLimit = 100u;
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070040} // anonymous namespace
41
Jamie Madill9e54b5a2016-05-25 12:57:39 -040042namespace rx
43{
44
Jamie Madille09bd5d2016-11-29 16:20:35 -050045namespace
46{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040047// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
48// one for the vertex shader.
49constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040050// Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes)
51static constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60;
Jamie Madille09bd5d2016-11-29 16:20:35 -050052
Omar El Sheikh26c61b22018-06-29 12:50:59 -060053bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
54{
55#if !defined(ANGLE_PLATFORM_ANDROID)
56 // Mock ICD does not currently run on Android
57 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
58 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
59 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
60#else
61 return false;
62#endif // !defined(ANGLE_PLATFORM_ANDROID)
63}
64
Jamie Madille09bd5d2016-11-29 16:20:35 -050065VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
66 const std::vector<const char *> &enabledExtensionNames)
67{
68 // Compile the extensions names into a set.
69 std::set<std::string> extensionNames;
70 for (const auto &extensionProp : extensionProps)
71 {
72 extensionNames.insert(extensionProp.extensionName);
73 }
74
Jamie Madillacf2f3a2017-11-21 19:22:44 -050075 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050076 {
77 if (extensionNames.count(extensionName) == 0)
78 {
79 return VK_ERROR_EXTENSION_NOT_PRESENT;
80 }
81 }
82
83 return VK_SUCCESS;
84}
85
Tobin Ehlis3a181e32018-08-29 15:17:05 -060086// Array of Validation error/warning messages that will be ignored, should include bugID
87constexpr std::array<const char *, 1> kSkippedMessages = {
88 // http://anglebug.com/2796
89 " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) "
90 "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader "
91 "corresponding to VK_SHADER_STAGE_VERTEX_BIT."};
92
93// Suppress validation errors that are known
94// return "true" if given code/prefix/message is known, else return "false"
95bool IsIgnoredDebugMessage(const char *message)
96{
97 for (const auto &msg : kSkippedMessages)
98 {
99 if (strcmp(msg, message) == 0)
100 {
101 return true;
102 }
103 }
104 return false;
105}
106
Yuly Novikov199f4292018-01-19 19:04:05 -0500107VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
108 VkDebugReportObjectTypeEXT objectType,
109 uint64_t object,
110 size_t location,
111 int32_t messageCode,
112 const char *layerPrefix,
113 const char *message,
114 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500115{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600116 if (IsIgnoredDebugMessage(message))
117 {
118 return VK_FALSE;
119 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500120 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
121 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500122 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500123#if !defined(NDEBUG)
124 // Abort the call in Debug builds.
125 return VK_TRUE;
126#endif
127 }
128 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
129 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500130 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500131 }
132 else
133 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500134 // Uncomment this if you want Vulkan spam.
135 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500136 }
137
138 return VK_FALSE;
139}
140
Yuly Novikov199f4292018-01-19 19:04:05 -0500141// If we're loading the validation layers, we could be running from any random directory.
142// Change to the executable directory so we can find the layers, then change back to the
143// previous directory to be safe we don't disrupt the application.
144class ScopedVkLoaderEnvironment : angle::NonCopyable
145{
146 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600147 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
148 : mEnableValidationLayers(enableValidationLayers),
149 mEnableMockICD(enableMockICD),
150 mChangedCWD(false),
151 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500152 {
153// Changing CWD and setting environment variables makes no sense on Android,
154// since this code is a part of Java application there.
155// Android Vulkan loader doesn't need this either.
156#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600157 if (enableMockICD)
158 {
159 // Override environment variable to use built Mock ICD
160 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
161 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
162 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
163 if (!mChangedICDPath)
164 {
165 ERR() << "Error setting Path for Mock/Null Driver.";
166 mEnableMockICD = false;
167 }
168 }
Jamie Madill46848422018-08-09 10:46:06 -0400169 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500170 {
171 const auto &cwd = angle::GetCWD();
172 if (!cwd.valid())
173 {
174 ERR() << "Error getting CWD for Vulkan layers init.";
175 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400176 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500177 }
178 else
179 {
180 mPreviousCWD = cwd.value();
181 const char *exeDir = angle::GetExecutableDirectory();
182 mChangedCWD = angle::SetCWD(exeDir);
183 if (!mChangedCWD)
184 {
185 ERR() << "Error setting CWD for Vulkan layers init.";
186 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400187 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500188 }
189 }
190 }
191
192 // Override environment variable to use the ANGLE layers.
193 if (mEnableValidationLayers)
194 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700195 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500196 {
197 ERR() << "Error setting environment for Vulkan layers init.";
198 mEnableValidationLayers = false;
199 }
200 }
201#endif // !defined(ANGLE_PLATFORM_ANDROID)
202 }
203
204 ~ScopedVkLoaderEnvironment()
205 {
206 if (mChangedCWD)
207 {
208#if !defined(ANGLE_PLATFORM_ANDROID)
209 ASSERT(mPreviousCWD.valid());
210 angle::SetCWD(mPreviousCWD.value().c_str());
211#endif // !defined(ANGLE_PLATFORM_ANDROID)
212 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600213 if (mChangedICDPath)
214 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600215 if (mPreviousICDPath.value().empty())
216 {
217 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
218 }
219 else
220 {
221 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
222 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600223 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500224 }
225
Jamie Madillaaca96e2018-06-12 10:19:48 -0400226 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500227
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600228 bool canEnableMockICD() const { return mEnableMockICD; }
229
Yuly Novikov199f4292018-01-19 19:04:05 -0500230 private:
231 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600232 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500233 bool mChangedCWD;
234 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600235 bool mChangedICDPath;
236 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500237};
238
Jamie Madill21061022018-07-12 23:56:30 -0400239void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
240 bool preferMockICD,
241 VkPhysicalDevice *physicalDeviceOut,
242 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
243{
244 ASSERT(!physicalDevices.empty());
245 if (preferMockICD)
246 {
247 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
248 {
249 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
250 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
251 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
252 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
253 {
254 *physicalDeviceOut = physicalDevice;
255 return;
256 }
257 }
258 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
259 "physicalDevice instead.";
260 }
261
262 // Fall back to first device.
263 *physicalDeviceOut = physicalDevices[0];
264 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
265}
Jamie Madill0da73fe2018-10-02 09:31:39 -0400266
267// Initially dumping the command graphs is disabled.
268constexpr bool kEnableCommandGraphDiagnostics = false;
Jamie Madille09bd5d2016-11-29 16:20:35 -0500269} // anonymous namespace
270
Jamie Madill49ac74b2017-12-21 14:42:33 -0500271// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400272RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500273
Jamie Madillaaca96e2018-06-12 10:19:48 -0400274RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500275
276RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
277 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
278{
279}
280
281RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
282{
283 std::swap(commandPool, other.commandPool);
284 std::swap(fence, other.fence);
285 std::swap(serial, other.serial);
286 return *this;
287}
288
Jamie Madillbea35a62018-07-05 11:54:10 -0400289void RendererVk::CommandBatch::destroy(VkDevice device)
290{
291 commandPool.destroy(device);
292 fence.destroy(device);
293}
294
Jamie Madill9f2a8612017-11-30 12:43:09 -0500295// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500296RendererVk::RendererVk()
297 : mCapsInitialized(false),
298 mInstance(VK_NULL_HANDLE),
299 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500300 mDebugReportCallback(VK_NULL_HANDLE),
301 mPhysicalDevice(VK_NULL_HANDLE),
302 mQueue(VK_NULL_HANDLE),
303 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
304 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400305 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400306 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400307 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400308 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400309 mCommandGraph(kEnableCommandGraphDiagnostics),
310 mGpuEventsEnabled(false),
311 mGpuClockSync{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()},
312 mGpuEventTimestampOrigin(0)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400313{
314}
315
316RendererVk::~RendererVk()
317{
Jamie Madill21061022018-07-12 23:56:30 -0400318}
319
320void RendererVk::onDestroy(vk::Context *context)
321{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500322 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500323 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500324 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400325 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500326 }
327
Jamie Madillc7918ce2018-06-13 13:25:31 -0400328 mPipelineLayoutCache.destroy(mDevice);
329 mDescriptorSetLayoutCache.destroy(mDevice);
330
Jamie Madill9f2a8612017-11-30 12:43:09 -0500331 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500332 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400333 mPipelineCacheVk.destroy(mDevice);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400334 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400335 mShaderLibrary.destroy(mDevice);
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400336 mGpuEventQueryPool.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500337
Jamie Madill06ca6342018-07-12 15:56:53 -0400338 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500339
Jamie Madill5deea722017-02-16 10:44:46 -0500340 if (mCommandPool.valid())
341 {
342 mCommandPool.destroy(mDevice);
343 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500344
345 if (mDevice)
346 {
347 vkDestroyDevice(mDevice, nullptr);
348 mDevice = VK_NULL_HANDLE;
349 }
350
Jamie Madill0448ec82016-12-23 13:41:47 -0500351 if (mDebugReportCallback)
352 {
353 ASSERT(mInstance);
354 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
355 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
356 ASSERT(destroyDebugReportCallback);
357 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
358 }
359
Jamie Madill4d0bf552016-12-28 15:45:24 -0500360 if (mInstance)
361 {
362 vkDestroyInstance(mInstance, nullptr);
363 mInstance = VK_NULL_HANDLE;
364 }
365
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600366 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500367 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500368}
369
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400370void RendererVk::markDeviceLost()
371{
372 mDeviceLost = true;
373}
374
375bool RendererVk::isDeviceLost() const
376{
377 return mDeviceLost;
378}
379
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400380angle::Result RendererVk::initialize(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400381 const egl::AttributeMap &attribs,
382 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500383{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600384 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
385 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500386 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600387 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500388
Jamie Madill0448ec82016-12-23 13:41:47 -0500389 // Gather global layer properties.
390 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400391 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500392
393 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
394 if (instanceLayerCount > 0)
395 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400396 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
397 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500398 }
399
Jamie Madille09bd5d2016-11-29 16:20:35 -0500400 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400401 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400402 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500403
404 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
405 if (instanceExtensionCount > 0)
406 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400407 ANGLE_VK_TRY(displayVk,
408 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
409 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500410 }
411
Yuly Novikov199f4292018-01-19 19:04:05 -0500412 const char *const *enabledLayerNames = nullptr;
413 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500414 if (mEnableValidationLayers)
415 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500416 bool layersRequested =
417 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
418 mEnableValidationLayers = GetAvailableValidationLayers(
419 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500420 }
421
Jamie Madille09bd5d2016-11-29 16:20:35 -0500422 std::vector<const char *> enabledInstanceExtensions;
423 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500424 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500425
Jamie Madill0448ec82016-12-23 13:41:47 -0500426 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
427 if (mEnableValidationLayers)
428 {
429 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
430 }
431
Jamie Madille09bd5d2016-11-29 16:20:35 -0500432 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400433 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400434 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500435
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400436 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500437 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500438 applicationInfo.pApplicationName = "ANGLE";
439 applicationInfo.applicationVersion = 1;
440 applicationInfo.pEngineName = "ANGLE";
441 applicationInfo.engineVersion = 1;
442 applicationInfo.apiVersion = VK_API_VERSION_1_0;
443
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400444 VkInstanceCreateInfo instanceInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500445 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500446 instanceInfo.flags = 0;
447 instanceInfo.pApplicationInfo = &applicationInfo;
448
Jamie Madille09bd5d2016-11-29 16:20:35 -0500449 // Enable requested layers and extensions.
450 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
451 instanceInfo.ppEnabledExtensionNames =
452 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500453 instanceInfo.enabledLayerCount = enabledLayerCount;
454 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500455
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400456 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500457
Jamie Madill0448ec82016-12-23 13:41:47 -0500458 if (mEnableValidationLayers)
459 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400460 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500461
462 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500463 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
464 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
465 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
466 debugReportInfo.pfnCallback = &DebugReportCallback;
467 debugReportInfo.pUserData = this;
468
469 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
470 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
471 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400472 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
473 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500474 }
475
Jamie Madill4d0bf552016-12-28 15:45:24 -0500476 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400477 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
478 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500479
480 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700481 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400482 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
483 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600484 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700485 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500486
Jamie Madill30b5d842018-08-31 17:19:12 -0400487 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
488
Jamie Madill4d0bf552016-12-28 15:45:24 -0500489 // Ensure we can find a graphics queue family.
490 uint32_t queueCount = 0;
491 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
492
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400493 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500494
495 mQueueFamilyProperties.resize(queueCount);
496 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
497 mQueueFamilyProperties.data());
498
499 size_t graphicsQueueFamilyCount = false;
500 uint32_t firstGraphicsQueueFamily = 0;
501 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
502 {
503 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
504 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
505 {
506 ASSERT(queueInfo.queueCount > 0);
507 graphicsQueueFamilyCount++;
508 if (firstGraphicsQueueFamily == 0)
509 {
510 firstGraphicsQueueFamily = familyIndex;
511 }
512 break;
513 }
514 }
515
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400516 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500517
Jamie Madill12222072018-07-11 14:59:48 -0400518 initFeatures();
519
Jamie Madill4d0bf552016-12-28 15:45:24 -0500520 // If only one queue family, go ahead and initialize the device. If there is more than one
521 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
522 if (graphicsQueueFamilyCount == 1)
523 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400524 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500525 }
526
Jamie Madill035fd6b2017-10-03 15:43:22 -0400527 // Store the physical device memory properties so we can find the right memory pools.
528 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500529
Jamie Madill06ca6342018-07-12 15:56:53 -0400530 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500531
Jamie Madill6a89d222017-11-02 11:59:51 -0400532 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500533 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
534 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400535
Jamie Madill21061022018-07-12 23:56:30 -0400536 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400537}
538
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400539angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500540{
541 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400542 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400543 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500544
545 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
546 if (deviceLayerCount > 0)
547 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400548 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
549 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500550 }
551
552 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400553 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
554 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500555
556 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
557 if (deviceExtensionCount > 0)
558 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400559 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
560 &deviceExtensionCount,
561 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500562 }
563
Yuly Novikov199f4292018-01-19 19:04:05 -0500564 const char *const *enabledLayerNames = nullptr;
565 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500566 if (mEnableValidationLayers)
567 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500568 mEnableValidationLayers = GetAvailableValidationLayers(
569 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500570 }
571
572 std::vector<const char *> enabledDeviceExtensions;
573 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
574
Luc Ferronbf6dc372018-06-28 15:24:19 -0400575 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
576 if (getFeatures().flipViewportY)
577 {
578 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
579 }
580
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400581 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500582
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400583 // Select additional features to be enabled
584 VkPhysicalDeviceFeatures enabledFeatures = {};
585 enabledFeatures.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
586
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400587 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500588
589 float zeroPriority = 0.0f;
590
591 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500592 queueCreateInfo.flags = 0;
593 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
594 queueCreateInfo.queueCount = 1;
595 queueCreateInfo.pQueuePriorities = &zeroPriority;
596
597 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400598 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500599
Jamie Madill50cf2be2018-06-15 09:46:57 -0400600 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400601 createInfo.flags = 0;
602 createInfo.queueCreateInfoCount = 1;
603 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500604 createInfo.enabledLayerCount = enabledLayerCount;
605 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500606 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
607 createInfo.ppEnabledExtensionNames =
608 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400609 createInfo.pEnabledFeatures = &enabledFeatures;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500610
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400611 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500612
613 mCurrentQueueFamilyIndex = queueFamilyIndex;
614
615 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
616
617 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400618 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500619 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500620 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
622
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400623 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
624
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400625 // Initialize the vulkan pipeline cache.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400626 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500627
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400628 // Initialize the submission semaphore pool.
629 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
630
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400631#if ANGLE_ENABLE_VULKAN_GPU_TRACE_EVENTS
632 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
633 ASSERT(platform);
634
635 // GPU tracing workaround for anglebug.com/2927. The renderer should not emit gpu events during
636 // platform discovery.
637 const unsigned char *gpuEventsEnabled =
638 platform->getTraceCategoryEnabledFlag(platform, "gpu.angle.gpu");
639 mGpuEventsEnabled = gpuEventsEnabled && *gpuEventsEnabled;
640#endif
641
642 if (mGpuEventsEnabled)
643 {
644 // Calculate the difference between CPU and GPU clocks for GPU event reporting.
645 ANGLE_TRY(mGpuEventQueryPool.init(displayVk, VK_QUERY_TYPE_TIMESTAMP,
646 vk::kDefaultTimestampQueryPoolSize));
647 ANGLE_TRY(synchronizeCpuGpuTime(displayVk));
648 }
649
Jamie Madill21061022018-07-12 23:56:30 -0400650 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500651}
652
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400653angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400654 VkSurfaceKHR surface,
655 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500656{
657 // We've already initialized a device, and can't re-create it unless it's never been used.
658 // TODO(jmadill): Handle the re-creation case if necessary.
659 if (mDevice != VK_NULL_HANDLE)
660 {
661 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
662
663 // Check if the current device supports present on this surface.
664 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400665 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400666 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500667 surface, &supportsPresent));
668
Jamie Madill6cad7732018-07-11 09:01:17 -0400669 if (supportsPresent == VK_TRUE)
670 {
671 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400672 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400673 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500674 }
675
676 // Find a graphics and present queue.
677 Optional<uint32_t> newPresentQueue;
678 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
679 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
680 {
681 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
682 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
683 {
684 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400685 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
686 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500687
688 if (supportsPresent == VK_TRUE)
689 {
690 newPresentQueue = queueIndex;
691 break;
692 }
693 }
694 }
695
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400696 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
697 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500698
Jamie Madill6cad7732018-07-11 09:01:17 -0400699 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400700 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500701}
702
703std::string RendererVk::getVendorString() const
704{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300705 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500706}
707
Jamie Madille09bd5d2016-11-29 16:20:35 -0500708std::string RendererVk::getRendererDescription() const
709{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500710 std::stringstream strstr;
711
712 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
713
714 strstr << "Vulkan ";
715 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
716 strstr << VK_VERSION_MINOR(apiVersion) << ".";
717 strstr << VK_VERSION_PATCH(apiVersion);
718
Olli Etuahoc6a06182018-04-13 14:11:46 +0300719 strstr << "(";
720
721 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
722 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
723 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
724 // driver detection.
725 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
726 {
727 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
728 }
729
730 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500731
732 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500733}
734
Jamie Madill12222072018-07-11 14:59:48 -0400735void RendererVk::initFeatures()
736{
Jamie Madillb36a4812018-09-25 10:15:11 -0400737// Use OpenGL line rasterization rules by default.
738// TODO(jmadill): Fix Android support. http://anglebug.com/2830
739#if defined(ANGLE_PLATFORM_ANDROID)
740 mFeatures.basicGLLineRasterization = false;
741#else
Jamie Madill12222072018-07-11 14:59:48 -0400742 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400743#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400744
Luc Ferronf786b702018-07-10 11:01:43 -0400745 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
746 // investigation. http://anglebug.com/2728
747 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400748
749#ifdef ANGLE_PLATFORM_WINDOWS
750 // http://anglebug.com/2838
751 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
752#endif
Jamie Madill12222072018-07-11 14:59:48 -0400753}
754
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400755void RendererVk::initPipelineCacheVkKey()
756{
757 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
758 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
759 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
760 // there is no '\0' in the result.
761 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
762 {
763 hashStream << std::hex << c;
764 }
765 // Add the vendor and device id too for good measure.
766 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
767 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
768
769 const std::string &hashString = hashStream.str();
770 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
771 hashString.length(), mPipelineCacheVkBlobKey.data());
772}
773
774angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
775{
776 initPipelineCacheVkKey();
777
778 egl::BlobCache::Value initialData;
779 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
780 mPipelineCacheVkBlobKey, &initialData);
781
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400782 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400783
784 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400785 pipelineCacheCreateInfo.flags = 0;
786 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
787 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
788
789 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
790 return angle::Result::Continue();
791}
792
Jamie Madillacccc6c2016-05-03 17:22:10 -0400793void RendererVk::ensureCapsInitialized() const
794{
795 if (!mCapsInitialized)
796 {
Shahbaz Youssefic2b576d2018-10-12 14:45:34 -0400797 ASSERT(mCurrentQueueFamilyIndex < mQueueFamilyProperties.size());
798 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures,
799 mQueueFamilyProperties[mCurrentQueueFamilyIndex], mNativeTextureCaps,
Jamie Madill30b5d842018-08-31 17:19:12 -0400800 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400801 mCapsInitialized = true;
802 }
803}
804
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400805void RendererVk::getSubmitWaitSemaphores(
806 vk::Context *context,
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400807 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores,
808 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> *waitStageMasks)
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400809{
810 if (mSubmitLastSignaledSemaphore.getSemaphore())
811 {
812 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400813 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400814
815 // Return the semaphore to the pool (which will remain valid and unused until the
816 // queue it's about to be waited on has finished execution).
817 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
818 }
819
820 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
821 {
822 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400823 waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
824
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400825 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
826 }
827 mSubmitWaitSemaphores.clear();
828}
829
Jamie Madillacccc6c2016-05-03 17:22:10 -0400830const gl::Caps &RendererVk::getNativeCaps() const
831{
832 ensureCapsInitialized();
833 return mNativeCaps;
834}
835
836const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
837{
838 ensureCapsInitialized();
839 return mNativeTextureCaps;
840}
841
842const gl::Extensions &RendererVk::getNativeExtensions() const
843{
844 ensureCapsInitialized();
845 return mNativeExtensions;
846}
847
848const gl::Limitations &RendererVk::getNativeLimitations() const
849{
850 ensureCapsInitialized();
851 return mNativeLimitations;
852}
853
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400854uint32_t RendererVk::getMaxActiveTextures()
855{
856 // TODO(lucferron): expose this limitation to GL in Context Caps
857 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
858 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
859}
860
Jamie Madill49ac74b2017-12-21 14:42:33 -0500861const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500862{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500863 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500864}
865
Jamie Madill21061022018-07-12 23:56:30 -0400866angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500867{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500868 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500869 {
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400870 TRACE_EVENT0("gpu.angle", "RendererVk::finish");
871
Luc Ferron1617e692018-07-11 11:08:19 -0400872 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
873 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400874
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400875 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400876 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
877 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400878
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400879 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500880 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400881 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
882 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400883 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500884 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400885 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500886 submitInfo.signalSemaphoreCount = 0;
887 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500888
Jamie Madill21061022018-07-12 23:56:30 -0400889 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500890 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500891
Jamie Madill4c26fc22017-02-24 11:04:10 -0500892 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400893 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400894 freeAllInFlightResources();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400895
896 if (mGpuEventsEnabled)
897 {
Shahbaz Youssefi749589f2018-10-25 12:48:49 -0400898 // This loop should in practice execute once since the queue is already idle.
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400899 while (mInFlightGpuEventQueries.size() > 0)
900 {
901 ANGLE_TRY(checkCompletedGpuEvents(context));
902 }
Shahbaz Youssefi749589f2018-10-25 12:48:49 -0400903 // Recalculate the CPU/GPU time difference to account for clock drifting. Avoid unnecessary
904 // synchronization if there is no event to be adjusted (happens when finish() gets called
905 // multiple times towards the end of the application).
906 if (mGpuEvents.size() > 0)
907 {
908 ANGLE_TRY(synchronizeCpuGpuTime(context));
909 }
Shahbaz Youssefi25224e72018-10-22 11:56:02 -0400910 }
911
Jamie Madill21061022018-07-12 23:56:30 -0400912 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500913}
914
Jamie Madill0c0dc342017-03-24 14:18:51 -0400915void RendererVk::freeAllInFlightResources()
916{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500917 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400918 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500919 batch.fence.destroy(mDevice);
920 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400921 }
922 mInFlightCommands.clear();
923
924 for (auto &garbage : mGarbage)
925 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400926 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400927 }
928 mGarbage.clear();
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400929
930 mLastCompletedQueueSerial = mLastSubmittedQueueSerial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400931}
932
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400933angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500934{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500935 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500936
Jamie Madill49ac74b2017-12-21 14:42:33 -0500937 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500938 {
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400939 angle::Result result = batch.fence.getStatus(context);
940 ANGLE_TRY(result);
941 if (result == angle::Result::Incomplete())
Jamie Madill0c0dc342017-03-24 14:18:51 -0400942 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500943
Jamie Madill49ac74b2017-12-21 14:42:33 -0500944 ASSERT(batch.serial > mLastCompletedQueueSerial);
945 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400946
Jamie Madill49ac74b2017-12-21 14:42:33 -0500947 batch.fence.destroy(mDevice);
948 batch.commandPool.destroy(mDevice);
949 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500950 }
951
Jamie Madill49ac74b2017-12-21 14:42:33 -0500952 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400953
954 size_t freeIndex = 0;
955 for (; freeIndex < mGarbage.size(); ++freeIndex)
956 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500957 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400958 break;
959 }
960
961 // Remove the entries from the garbage list - they should be ready to go.
962 if (freeIndex > 0)
963 {
964 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500965 }
966
Jamie Madill21061022018-07-12 23:56:30 -0400967 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500968}
969
Jamie Madill21061022018-07-12 23:56:30 -0400970angle::Result RendererVk::submitFrame(vk::Context *context,
971 const VkSubmitInfo &submitInfo,
972 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500973{
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400974 VkFenceCreateInfo fenceInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500975 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500976 fenceInfo.flags = 0;
977
Jamie Madillbea35a62018-07-05 11:54:10 -0400978 vk::Scoped<CommandBatch> scopedBatch(mDevice);
979 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400980 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500981
Jamie Madill21061022018-07-12 23:56:30 -0400982 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500983
984 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500985 batch.commandPool = std::move(mCommandPool);
986 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500987
Jamie Madillbea35a62018-07-05 11:54:10 -0400988 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400989
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400990 // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on
991 // swap() though, and there could be multiple submissions in between (through glFlush() calls),
992 // so the limit is larger than the expected number of images.
993 ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400994
995 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400996 // TODO(jmadill): Overflow check.
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400997 mLastSubmittedQueueSerial = mCurrentQueueSerial;
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400998 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400999
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001000 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -04001001
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001002 if (mGpuEventsEnabled)
1003 {
1004 ANGLE_TRY(checkCompletedGpuEvents(context));
1005 }
1006
Jamie Madill49ac74b2017-12-21 14:42:33 -05001007 // Simply null out the command buffer here - it was allocated using the command pool.
1008 commandBuffer.releaseHandle();
1009
1010 // Reallocate the command pool for next frame.
1011 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001012 VkCommandPoolCreateInfo poolInfo = {};
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001013 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001014 poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001015 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001016
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001017 return mCommandPool.init(context, poolInfo);
Jamie Madill4c26fc22017-02-24 11:04:10 -05001018}
1019
Jamie Madillaaca96e2018-06-12 10:19:48 -04001020bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -05001021{
1022 return serial > mLastCompletedQueueSerial;
1023}
1024
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -04001025angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
1026{
1027 if (!isSerialInUse(serial) || mInFlightCommands.empty())
1028 {
1029 return angle::Result::Continue();
1030 }
1031
1032 // Find the first batch with serial equal to or bigger than given serial (note that
1033 // the batch serials are unique, otherwise upper-bound would have been necessary).
1034 size_t batchIndex = mInFlightCommands.size() - 1;
1035 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
1036 {
1037 if (mInFlightCommands[i].serial >= serial)
1038 {
1039 batchIndex = i;
1040 break;
1041 }
1042 }
1043 const CommandBatch &batch = mInFlightCommands[batchIndex];
1044
1045 // Wait for it finish
1046 constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
1047 angle::Result result = batch.fence.wait(context, kMaxFenceWaitTimeNs);
1048 if (result == angle::Result::Incomplete())
1049 {
1050 // Wait a maximum of 10s. If that times out, we declare it a failure.
1051 result = angle::Result::Stop();
1052 }
1053 ANGLE_TRY(result);
1054
1055 // Clean up finished batches.
1056 return checkCompletedCommands(context);
1057}
1058
Jamie Madill21061022018-07-12 23:56:30 -04001059angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1060 const vk::RenderPassDesc &desc,
1061 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001062{
Jamie Madill21061022018-07-12 23:56:30 -04001063 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001064 renderPassOut);
1065}
1066
Jamie Madill21061022018-07-12 23:56:30 -04001067angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1068 const vk::RenderPassDesc &desc,
1069 const vk::AttachmentOpsArray &ops,
1070 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001071{
Jamie Madill21061022018-07-12 23:56:30 -04001072 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001073 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001074}
1075
Jamie Madilla5e06072018-05-18 14:36:05 -04001076vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001077{
Jamie Madilla5e06072018-05-18 14:36:05 -04001078 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001079}
1080
Jamie Madill21061022018-07-12 23:56:30 -04001081angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001082{
Jamie Madill21061022018-07-12 23:56:30 -04001083 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001084 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001085}
1086
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001087angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001088{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001089 if (mCommandGraph.empty())
1090 {
1091 return angle::Result::Continue();
1092 }
1093
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001094 TRACE_EVENT0("gpu.angle", "RendererVk::flush");
1095
Jamie Madillbea35a62018-07-05 11:54:10 -04001096 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1097 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001098
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001099 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001100 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1101 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001102
1103 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1104 // will be waited on.
1105 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001106
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001107 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001108 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001109 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1110 submitInfo.pWaitSemaphores = waitSemaphores.data();
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001111 submitInfo.pWaitDstStageMask = waitStageMasks.data();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001112 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001113 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001114 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001115 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001116
Jamie Madill21061022018-07-12 23:56:30 -04001117 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001118
Jamie Madill21061022018-07-12 23:56:30 -04001119 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001120}
1121
Jamie Madill78feddc2018-04-27 11:45:05 -04001122Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001123{
Jamie Madill78feddc2018-04-27 11:45:05 -04001124 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001125}
1126
Jamie Madill06ca6342018-07-12 15:56:53 -04001127angle::Result RendererVk::getPipeline(vk::Context *context,
1128 const vk::ShaderAndSerial &vertexShader,
1129 const vk::ShaderAndSerial &fragmentShader,
1130 const vk::PipelineLayout &pipelineLayout,
1131 const vk::PipelineDesc &pipelineDesc,
1132 const gl::AttributesMask &activeAttribLocationsMask,
1133 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001134{
Jamie Madill06ca6342018-07-12 15:56:53 -04001135 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001136 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001137 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001138 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1139
1140 // Pull in a compatible RenderPass.
1141 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001142 ANGLE_TRY(
1143 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001144
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001145 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1146 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001147 fragmentShader.get(), pipelineDesc, pipelineOut);
1148}
1149
Jamie Madill21061022018-07-12 23:56:30 -04001150angle::Result RendererVk::getDescriptorSetLayout(
1151 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001152 const vk::DescriptorSetLayoutDesc &desc,
1153 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1154{
Jamie Madill21061022018-07-12 23:56:30 -04001155 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001156}
1157
Jamie Madill21061022018-07-12 23:56:30 -04001158angle::Result RendererVk::getPipelineLayout(
1159 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001160 const vk::PipelineLayoutDesc &desc,
1161 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1162 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1163{
Jamie Madill21061022018-07-12 23:56:30 -04001164 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001165 pipelineLayoutOut);
1166}
1167
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001168angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1169{
1170 ASSERT(mPipelineCacheVk.valid());
1171
1172 if (--mPipelineCacheVkUpdateTimeout > 0)
1173 {
1174 return angle::Result::Continue();
1175 }
1176
1177 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1178
1179 // Get the size of the cache.
1180 size_t pipelineCacheSize = 0;
1181 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1182
1183 angle::MemoryBuffer *pipelineCacheData = nullptr;
1184 ANGLE_VK_CHECK_ALLOC(displayVk,
1185 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1186
1187 size_t originalPipelineCacheSize = pipelineCacheSize;
1188 angle::Result result =
1189 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1190 ANGLE_TRY(result);
1191
1192 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1193 // was determined just above), so receiving it hints at an implementation bug we would want
1194 // to know about early.
1195 ASSERT(result != angle::Result::Incomplete());
1196
1197 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1198 // the buffer to avoid leaking garbage memory.
1199 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1200 if (pipelineCacheSize < originalPipelineCacheSize)
1201 {
1202 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1203 originalPipelineCacheSize - pipelineCacheSize);
1204 }
1205
1206 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1207
1208 return angle::Result::Continue();
1209}
1210
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001211angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1212 const vk::Semaphore **outSemaphore)
1213{
1214 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1215
1216 vk::SemaphoreHelper semaphore;
1217 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1218
1219 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1220 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1221
1222 return angle::Result::Continue();
1223}
1224
1225const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1226{
1227 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1228
1229 // Return the semaphore to the pool (which will remain valid and unused until the
1230 // queue it's about to be waited on has finished execution). The caller is about
1231 // to wait on it.
1232 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1233
1234 return semaphore;
1235}
1236
Jamie Madilld47044a2018-04-27 11:45:03 -04001237vk::ShaderLibrary *RendererVk::getShaderLibrary()
1238{
1239 return &mShaderLibrary;
1240}
Luc Ferron90968362018-05-04 08:47:22 -04001241
Shahbaz Youssefi749589f2018-10-25 12:48:49 -04001242angle::Result RendererVk::getTimestamp(vk::Context *context, uint64_t *timestampOut)
1243{
1244 // The intent of this function is to query the timestamp without stalling the GPU. Currently,
1245 // that seems impossible, so instead, we are going to make a small submission with just a
1246 // timestamp query. First, the disjoint timer query extension says:
1247 //
1248 // > This will return the GL time after all previous commands have reached the GL server but
1249 // have not yet necessarily executed.
1250 //
1251 // The previous commands are stored in the command graph at the moment and are not yet flushed.
1252 // The wording allows us to make a submission to get the timestamp without performing a flush.
1253 //
1254 // Second:
1255 //
1256 // > By using a combination of this synchronous get command and the asynchronous timestamp query
1257 // object target, applications can measure the latency between when commands reach the GL server
1258 // and when they are realized in the framebuffer.
1259 //
1260 // This fits with the above strategy as well, although inevitably we are possibly introducing a
1261 // GPU bubble. This function directly generates a command buffer and submits it instead of
1262 // using the other member functions. This is to avoid changing any state, such as the queue
1263 // serial.
1264
1265 // Create a query used to receive the GPU timestamp
1266 vk::Scoped<vk::DynamicQueryPool> timestampQueryPool(mDevice);
1267 vk::QueryHelper timestampQuery;
1268 ANGLE_TRY(timestampQueryPool.get().init(context, VK_QUERY_TYPE_TIMESTAMP, 1));
1269 ANGLE_TRY(timestampQueryPool.get().allocateQuery(context, &timestampQuery));
1270
1271 // Record the command buffer
1272 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1273 vk::CommandBuffer &commandBuffer = commandBatch.get();
1274
1275 VkCommandBufferAllocateInfo commandBufferInfo = {};
1276 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1277 commandBufferInfo.commandPool = mCommandPool.getHandle();
1278 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1279 commandBufferInfo.commandBufferCount = 1;
1280
1281 ANGLE_TRY(commandBuffer.init(context, commandBufferInfo));
1282
1283 VkCommandBufferBeginInfo beginInfo = {};
1284 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1285 beginInfo.flags = 0;
1286 beginInfo.pInheritanceInfo = nullptr;
1287
1288 ANGLE_TRY(commandBuffer.begin(context, beginInfo));
1289
1290 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1291 timestampQuery.getQuery(), 1);
1292 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1293 timestampQuery.getQueryPool()->getHandle(),
1294 timestampQuery.getQuery());
1295
1296 ANGLE_TRY(commandBuffer.end(context));
1297
1298 // Create fence for the submission
1299 VkFenceCreateInfo fenceInfo = {};
1300 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1301 fenceInfo.flags = 0;
1302
1303 vk::Scoped<vk::Fence> fence(mDevice);
1304 ANGLE_TRY(fence.get().init(context, fenceInfo));
1305
1306 // Submit the command buffer
1307 VkSubmitInfo submitInfo = {};
1308 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1309 submitInfo.waitSemaphoreCount = 0;
1310 submitInfo.pWaitSemaphores = nullptr;
1311 submitInfo.pWaitDstStageMask = nullptr;
1312 submitInfo.commandBufferCount = 1;
1313 submitInfo.pCommandBuffers = commandBuffer.ptr();
1314 submitInfo.signalSemaphoreCount = 0;
1315 submitInfo.pSignalSemaphores = nullptr;
1316
1317 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, fence.get().getHandle()));
1318
1319 // Wait for the submission to finish. Given no semaphores, there is hope that it would execute
1320 // in parallel with what's already running on the GPU.
1321 constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
1322 angle::Result result = fence.get().wait(context, kMaxFenceWaitTimeNs);
1323 if (result == angle::Result::Incomplete())
1324 {
1325 // Declare it a failure if it times out.
1326 result = angle::Result::Stop();
1327 }
1328 ANGLE_TRY(result);
1329
1330 // Get the query results
1331 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1332
1333 ANGLE_TRY(timestampQuery.getQueryPool()->getResults(context, timestampQuery.getQuery(), 1,
1334 sizeof(*timestampOut), timestampOut,
1335 sizeof(*timestampOut), queryFlags));
1336
1337 timestampQueryPool.get().freeQuery(context, &timestampQuery);
1338
1339 return angle::Result::Continue();
1340}
1341
Shahbaz Youssefi25224e72018-10-22 11:56:02 -04001342angle::Result RendererVk::synchronizeCpuGpuTime(vk::Context *context)
1343{
1344 ASSERT(mGpuEventsEnabled);
1345
1346 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1347 ASSERT(platform);
1348
1349 // To synchronize CPU and GPU times, we need to get the CPU timestamp as close as possible to
1350 // the GPU timestamp. The process of getting the GPU timestamp is as follows:
1351 //
1352 // CPU GPU
1353 //
1354 // Record command buffer
1355 // with timestamp query
1356 //
1357 // Submit command buffer
1358 //
1359 // Post-submission work Begin execution
1360 //
1361 // ???? Write timstamp Tgpu
1362 //
1363 // ???? End execution
1364 //
1365 // ???? Return query results
1366 //
1367 // ????
1368 //
1369 // Get query results
1370 //
1371 // The areas of unknown work (????) on the CPU indicate that the CPU may or may not have
1372 // finished post-submission work while the GPU is executing in parallel. With no further work,
1373 // querying CPU timestamps before submission and after getting query results give the bounds to
1374 // Tgpu, which could be quite large.
1375 //
1376 // Using VkEvents, the GPU can be made to wait for the CPU and vice versa, in an effort to
1377 // reduce this range. This function implements the following procedure:
1378 //
1379 // CPU GPU
1380 //
1381 // Record command buffer
1382 // with timestamp query
1383 //
1384 // Submit command buffer
1385 //
1386 // Post-submission work Begin execution
1387 //
1388 // ???? Set Event GPUReady
1389 //
1390 // Wait on Event GPUReady Wait on Event CPUReady
1391 //
1392 // Get CPU Time Ts Wait on Event CPUReady
1393 //
1394 // Set Event CPUReady Wait on Event CPUReady
1395 //
1396 // Get CPU Time Tcpu Get GPU Time Tgpu
1397 //
1398 // Wait on Event GPUDone Set Event GPUDone
1399 //
1400 // Get CPU Time Te End Execution
1401 //
1402 // Idle Return query results
1403 //
1404 // Get query results
1405 //
1406 // If Te-Ts > epsilon, a GPU or CPU interruption can be assumed and the operation can be
1407 // retried. Once Te-Ts < epsilon, Tcpu can be taken to presumably match Tgpu. Finding an
1408 // epsilon that's valid for all devices may be difficult, so the loop can be performed only a
1409 // limited number of times and the Tcpu,Tgpu pair corresponding to smallest Te-Ts used for
1410 // calibration.
1411 //
1412 // Note: Once VK_EXT_calibrated_timestamps is ubiquitous, this should be redone.
1413
1414 // Make sure nothing is running
1415 ASSERT(mCommandGraph.empty());
1416
1417 TRACE_EVENT0("gpu.angle", "RendererVk::synchronizeCpuGpuTime");
1418
1419 // Create a query used to receive the GPU timestamp
1420 vk::QueryHelper timestampQuery;
1421 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &timestampQuery));
1422
1423 // Create the three events
1424 VkEventCreateInfo eventCreateInfo = {};
1425 eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
1426 eventCreateInfo.flags = 0;
1427
1428 vk::Scoped<vk::Event> cpuReady(mDevice), gpuReady(mDevice), gpuDone(mDevice);
1429 ANGLE_TRY(cpuReady.get().init(context, eventCreateInfo));
1430 ANGLE_TRY(gpuReady.get().init(context, eventCreateInfo));
1431 ANGLE_TRY(gpuDone.get().init(context, eventCreateInfo));
1432
1433 constexpr uint32_t kRetries = 10;
1434
1435 // Time suffixes used are S for seconds and Cycles for cycles
1436 double tightestRangeS = 1e6f;
1437 double TcpuS = 0;
1438 uint64_t TgpuCycles = 0;
1439 for (uint32_t i = 0; i < kRetries; ++i)
1440 {
1441 // Reset the events
1442 ANGLE_TRY(cpuReady.get().reset(context));
1443 ANGLE_TRY(gpuReady.get().reset(context));
1444 ANGLE_TRY(gpuDone.get().reset(context));
1445
1446 // Record the command buffer
1447 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1448 vk::CommandBuffer &commandBuffer = commandBatch.get();
1449
1450 VkCommandBufferAllocateInfo commandBufferInfo = {};
1451 commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1452 commandBufferInfo.commandPool = mCommandPool.getHandle();
1453 commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1454 commandBufferInfo.commandBufferCount = 1;
1455
1456 ANGLE_TRY(commandBuffer.init(context, commandBufferInfo));
1457
1458 VkCommandBufferBeginInfo beginInfo = {};
1459 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1460 beginInfo.flags = 0;
1461 beginInfo.pInheritanceInfo = nullptr;
1462
1463 ANGLE_TRY(commandBuffer.begin(context, beginInfo));
1464
1465 commandBuffer.setEvent(gpuReady.get(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
1466 commandBuffer.waitEvents(1, cpuReady.get().ptr(), VK_PIPELINE_STAGE_HOST_BIT,
1467 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, nullptr, 0, nullptr, 0,
1468 nullptr);
1469
1470 commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(),
1471 timestampQuery.getQuery(), 1);
1472 commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1473 timestampQuery.getQueryPool()->getHandle(),
1474 timestampQuery.getQuery());
1475
1476 commandBuffer.setEvent(gpuDone.get(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
1477
1478 ANGLE_TRY(commandBuffer.end(context));
1479
1480 // Submit the command buffer
1481 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1482 angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks;
1483 getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks);
1484
1485 VkSubmitInfo submitInfo = {};
1486 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1487 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1488 submitInfo.pWaitSemaphores = waitSemaphores.data();
1489 submitInfo.pWaitDstStageMask = waitStageMasks.data();
1490 submitInfo.commandBufferCount = 1;
1491 submitInfo.pCommandBuffers = commandBuffer.ptr();
1492 submitInfo.signalSemaphoreCount = 0;
1493 submitInfo.pSignalSemaphores = nullptr;
1494
1495 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBuffer)));
1496
1497 // Wait for GPU to be ready. This is a short busy wait.
1498 angle::Result result = angle::Result::Incomplete();
1499 do
1500 {
1501 result = gpuReady.get().getStatus(context);
1502 ANGLE_TRY(result);
1503 } while (result == angle::Result::Incomplete());
1504
1505 double TsS = platform->monotonicallyIncreasingTime(platform);
1506
1507 // Tell the GPU to go ahead with the timestamp query.
1508 ANGLE_TRY(cpuReady.get().set(context));
1509 double cpuTimestampS = platform->monotonicallyIncreasingTime(platform);
1510
1511 // Wait for GPU to be done. Another short busy wait.
1512 do
1513 {
1514 result = gpuDone.get().getStatus(context);
1515 ANGLE_TRY(result);
1516 } while (result == angle::Result::Incomplete());
1517
1518 double TeS = platform->monotonicallyIncreasingTime(platform);
1519
1520 // Get the query results
1521 ANGLE_TRY(finishToSerial(context, getLastSubmittedQueueSerial()));
1522
1523 constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT;
1524
1525 uint64_t gpuTimestampCycles = 0;
1526 ANGLE_TRY(timestampQuery.getQueryPool()->getResults(
1527 context, timestampQuery.getQuery(), 1, sizeof(gpuTimestampCycles), &gpuTimestampCycles,
1528 sizeof(gpuTimestampCycles), queryFlags));
1529
1530 // Use the first timestamp queried as origin.
1531 if (mGpuEventTimestampOrigin == 0)
1532 {
1533 mGpuEventTimestampOrigin = gpuTimestampCycles;
1534 }
1535
1536 // Take these CPU and GPU timestamps if there is better confidence.
1537 double confidenceRangeS = TeS - TsS;
1538 if (confidenceRangeS < tightestRangeS)
1539 {
1540 tightestRangeS = confidenceRangeS;
1541 TcpuS = cpuTimestampS;
1542 TgpuCycles = gpuTimestampCycles;
1543 }
1544 }
1545
1546 mGpuEventQueryPool.freeQuery(context, &timestampQuery);
1547
1548 // timestampPeriod gives nanoseconds/cycle.
1549 double TgpuS = (TgpuCycles - mGpuEventTimestampOrigin) *
1550 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) /
1551 1'000'000'000.0;
1552
1553 flushGpuEvents(TgpuS, TcpuS);
1554
1555 mGpuClockSync.gpuTimestampS = TgpuS;
1556 mGpuClockSync.cpuTimestampS = TcpuS;
1557
1558 return angle::Result::Continue();
1559}
1560
1561angle::Result RendererVk::traceGpuEventImpl(vk::Context *context,
1562 vk::CommandBuffer *commandBuffer,
1563 char phase,
1564 const char *name)
1565{
1566 ASSERT(mGpuEventsEnabled);
1567
1568 GpuEventQuery event;
1569
1570 event.name = name;
1571 event.phase = phase;
1572 event.serial = mCurrentQueueSerial;
1573
1574 ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &event.queryPoolIndex, &event.queryIndex));
1575
1576 commandBuffer->resetQueryPool(
1577 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex, 1);
1578 commandBuffer->writeTimestamp(
1579 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1580 mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex);
1581
1582 mInFlightGpuEventQueries.push_back(std::move(event));
1583
1584 return angle::Result::Continue();
1585}
1586
1587angle::Result RendererVk::checkCompletedGpuEvents(vk::Context *context)
1588{
1589 ASSERT(mGpuEventsEnabled);
1590
1591 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1592 ASSERT(platform);
1593
1594 int finishedCount = 0;
1595
1596 for (GpuEventQuery &eventQuery : mInFlightGpuEventQueries)
1597 {
1598 // Only check the timestamp query if the submission has finished.
1599 if (eventQuery.serial > mLastCompletedQueueSerial)
1600 {
1601 break;
1602 }
1603
1604 // See if the results are available.
1605 uint64_t gpuTimestampCycles = 0;
1606 angle::Result result = mGpuEventQueryPool.getQueryPool(eventQuery.queryPoolIndex)
1607 ->getResults(context, eventQuery.queryIndex, 1,
1608 sizeof(gpuTimestampCycles), &gpuTimestampCycles,
1609 sizeof(gpuTimestampCycles), VK_QUERY_RESULT_64_BIT);
1610 ANGLE_TRY(result);
1611
1612 if (result == angle::Result::Incomplete())
1613 {
1614 break;
1615 }
1616
1617 mGpuEventQueryPool.freeQuery(context, eventQuery.queryPoolIndex, eventQuery.queryIndex);
1618
1619 GpuEvent event;
1620 event.gpuTimestampCycles = gpuTimestampCycles;
1621 event.name = eventQuery.name;
1622 event.phase = eventQuery.phase;
1623
1624 mGpuEvents.emplace_back(event);
1625
1626 ++finishedCount;
1627 }
1628
1629 mInFlightGpuEventQueries.erase(mInFlightGpuEventQueries.begin(),
1630 mInFlightGpuEventQueries.begin() + finishedCount);
1631
1632 return angle::Result::Continue();
1633}
1634
1635void RendererVk::flushGpuEvents(double nextSyncGpuTimestampS, double nextSyncCpuTimestampS)
1636{
1637 if (mGpuEvents.size() == 0)
1638 {
1639 return;
1640 }
1641
1642 angle::PlatformMethods *platform = ANGLEPlatformCurrent();
1643 ASSERT(platform);
1644
1645 // Find the slope of the clock drift for adjustment
1646 double lastGpuSyncTimeS = mGpuClockSync.gpuTimestampS;
1647 double lastGpuSyncDiffS = mGpuClockSync.cpuTimestampS - mGpuClockSync.gpuTimestampS;
1648 double gpuSyncDriftSlope = 0;
1649
1650 double nextGpuSyncTimeS = nextSyncGpuTimestampS;
1651 double nextGpuSyncDiffS = nextSyncCpuTimestampS - nextSyncGpuTimestampS;
1652
1653 // No gpu trace events should have been generated before the clock sync, so if there is no
1654 // "previous" clock sync, there should be no gpu events (i.e. the function early-outs above).
1655 ASSERT(mGpuClockSync.gpuTimestampS != std::numeric_limits<double>::max() &&
1656 mGpuClockSync.cpuTimestampS != std::numeric_limits<double>::max());
1657
1658 gpuSyncDriftSlope =
1659 (nextGpuSyncDiffS - lastGpuSyncDiffS) / (nextGpuSyncTimeS - lastGpuSyncTimeS);
1660
1661 for (const GpuEvent &event : mGpuEvents)
1662 {
1663 double gpuTimestampS =
1664 (event.gpuTimestampCycles - mGpuEventTimestampOrigin) *
1665 static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) * 1e-9;
1666
1667 // Account for clock drift.
1668 gpuTimestampS += lastGpuSyncDiffS + gpuSyncDriftSlope * (gpuTimestampS - lastGpuSyncTimeS);
1669
1670 // Generate the trace now that the GPU timestamp is available and clock drifts are accounted
1671 // for.
1672 static long long eventId = 1;
1673 static const unsigned char *categoryEnabled =
1674 TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu.angle.gpu");
1675 platform->addTraceEvent(platform, event.phase, categoryEnabled, event.name, eventId++,
1676 gpuTimestampS, 0, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
1677 }
1678
1679 mGpuEvents.clear();
1680}
1681
Jamie Madillaaca96e2018-06-12 10:19:48 -04001682uint32_t GetUniformBufferDescriptorCount()
1683{
1684 return kUniformBufferDescriptorsPerDescriptorSet;
1685}
1686
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001687} // namespace rx