blob: bb08d76b693c35ef957f22f8cb84d72758d747bc [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"
22#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050023#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050024#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050025#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050026#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050027#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050028#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040029
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070030// Consts
31namespace
32{
33const uint32_t kMockVendorID = 0xba5eba11;
34const uint32_t kMockDeviceID = 0xf005ba11;
35constexpr char kMockDeviceName[] = "Vulkan Mock Device";
36} // anonymous namespace
37
Jamie Madill9e54b5a2016-05-25 12:57:39 -040038namespace rx
39{
40
Jamie Madille09bd5d2016-11-29 16:20:35 -050041namespace
42{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040043// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
44// one for the vertex shader.
45constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050046
Omar El Sheikh26c61b22018-06-29 12:50:59 -060047bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
48{
49#if !defined(ANGLE_PLATFORM_ANDROID)
50 // Mock ICD does not currently run on Android
51 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
52 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
53 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
54#else
55 return false;
56#endif // !defined(ANGLE_PLATFORM_ANDROID)
57}
58
Jamie Madille09bd5d2016-11-29 16:20:35 -050059VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
60 const std::vector<const char *> &enabledExtensionNames)
61{
62 // Compile the extensions names into a set.
63 std::set<std::string> extensionNames;
64 for (const auto &extensionProp : extensionProps)
65 {
66 extensionNames.insert(extensionProp.extensionName);
67 }
68
Jamie Madillacf2f3a2017-11-21 19:22:44 -050069 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050070 {
71 if (extensionNames.count(extensionName) == 0)
72 {
73 return VK_ERROR_EXTENSION_NOT_PRESENT;
74 }
75 }
76
77 return VK_SUCCESS;
78}
79
Yuly Novikov199f4292018-01-19 19:04:05 -050080VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
81 VkDebugReportObjectTypeEXT objectType,
82 uint64_t object,
83 size_t location,
84 int32_t messageCode,
85 const char *layerPrefix,
86 const char *message,
87 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050088{
89 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
90 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050091 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050092#if !defined(NDEBUG)
93 // Abort the call in Debug builds.
94 return VK_TRUE;
95#endif
96 }
97 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
98 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050099 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500100 }
101 else
102 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500103 // Uncomment this if you want Vulkan spam.
104 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500105 }
106
107 return VK_FALSE;
108}
109
Yuly Novikov199f4292018-01-19 19:04:05 -0500110// If we're loading the validation layers, we could be running from any random directory.
111// Change to the executable directory so we can find the layers, then change back to the
112// previous directory to be safe we don't disrupt the application.
113class ScopedVkLoaderEnvironment : angle::NonCopyable
114{
115 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600116 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
117 : mEnableValidationLayers(enableValidationLayers),
118 mEnableMockICD(enableMockICD),
119 mChangedCWD(false),
120 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500121 {
122// Changing CWD and setting environment variables makes no sense on Android,
123// since this code is a part of Java application there.
124// Android Vulkan loader doesn't need this either.
125#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600126 if (enableMockICD)
127 {
128 // Override environment variable to use built Mock ICD
129 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
130 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
131 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
132 if (!mChangedICDPath)
133 {
134 ERR() << "Error setting Path for Mock/Null Driver.";
135 mEnableMockICD = false;
136 }
137 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500138 if (mEnableValidationLayers)
139 {
140 const auto &cwd = angle::GetCWD();
141 if (!cwd.valid())
142 {
143 ERR() << "Error getting CWD for Vulkan layers init.";
144 mEnableValidationLayers = false;
145 }
146 else
147 {
148 mPreviousCWD = cwd.value();
149 const char *exeDir = angle::GetExecutableDirectory();
150 mChangedCWD = angle::SetCWD(exeDir);
151 if (!mChangedCWD)
152 {
153 ERR() << "Error setting CWD for Vulkan layers init.";
154 mEnableValidationLayers = false;
155 }
156 }
157 }
158
159 // Override environment variable to use the ANGLE layers.
160 if (mEnableValidationLayers)
161 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700162 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500163 {
164 ERR() << "Error setting environment for Vulkan layers init.";
165 mEnableValidationLayers = false;
166 }
167 }
168#endif // !defined(ANGLE_PLATFORM_ANDROID)
169 }
170
171 ~ScopedVkLoaderEnvironment()
172 {
173 if (mChangedCWD)
174 {
175#if !defined(ANGLE_PLATFORM_ANDROID)
176 ASSERT(mPreviousCWD.valid());
177 angle::SetCWD(mPreviousCWD.value().c_str());
178#endif // !defined(ANGLE_PLATFORM_ANDROID)
179 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600180 if (mChangedICDPath)
181 {
182 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
183 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500184 }
185
Jamie Madillaaca96e2018-06-12 10:19:48 -0400186 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500187
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600188 bool canEnableMockICD() const { return mEnableMockICD; }
189
Yuly Novikov199f4292018-01-19 19:04:05 -0500190 private:
191 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600192 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500193 bool mChangedCWD;
194 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600195 bool mChangedICDPath;
196 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500197};
198
Jamie Madill21061022018-07-12 23:56:30 -0400199void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
200 bool preferMockICD,
201 VkPhysicalDevice *physicalDeviceOut,
202 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
203{
204 ASSERT(!physicalDevices.empty());
205 if (preferMockICD)
206 {
207 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
208 {
209 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
210 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
211 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
212 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
213 {
214 *physicalDeviceOut = physicalDevice;
215 return;
216 }
217 }
218 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
219 "physicalDevice instead.";
220 }
221
222 // Fall back to first device.
223 *physicalDeviceOut = physicalDevices[0];
224 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
225}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500226} // anonymous namespace
227
Jamie Madill49ac74b2017-12-21 14:42:33 -0500228// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400229RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500230
Jamie Madillaaca96e2018-06-12 10:19:48 -0400231RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500232
233RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
234 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
235{
236}
237
238RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
239{
240 std::swap(commandPool, other.commandPool);
241 std::swap(fence, other.fence);
242 std::swap(serial, other.serial);
243 return *this;
244}
245
Jamie Madillbea35a62018-07-05 11:54:10 -0400246void RendererVk::CommandBatch::destroy(VkDevice device)
247{
248 commandPool.destroy(device);
249 fence.destroy(device);
250}
251
Jamie Madill9f2a8612017-11-30 12:43:09 -0500252// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500253RendererVk::RendererVk()
254 : mCapsInitialized(false),
255 mInstance(VK_NULL_HANDLE),
256 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500257 mDebugReportCallback(VK_NULL_HANDLE),
258 mPhysicalDevice(VK_NULL_HANDLE),
259 mQueue(VK_NULL_HANDLE),
260 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
261 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400262 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400263 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400264{
265}
266
267RendererVk::~RendererVk()
268{
Jamie Madill21061022018-07-12 23:56:30 -0400269}
270
271void RendererVk::onDestroy(vk::Context *context)
272{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500273 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500274 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500275 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400276 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500277 }
278
Jamie Madillc7918ce2018-06-13 13:25:31 -0400279 mPipelineLayoutCache.destroy(mDevice);
280 mDescriptorSetLayoutCache.destroy(mDevice);
281
Jamie Madill9f2a8612017-11-30 12:43:09 -0500282 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500283 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400284 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500285
Jamie Madill06ca6342018-07-12 15:56:53 -0400286 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500287
Jamie Madill5deea722017-02-16 10:44:46 -0500288 if (mCommandPool.valid())
289 {
290 mCommandPool.destroy(mDevice);
291 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500292
293 if (mDevice)
294 {
295 vkDestroyDevice(mDevice, nullptr);
296 mDevice = VK_NULL_HANDLE;
297 }
298
Jamie Madill0448ec82016-12-23 13:41:47 -0500299 if (mDebugReportCallback)
300 {
301 ASSERT(mInstance);
302 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
303 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
304 ASSERT(destroyDebugReportCallback);
305 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
306 }
307
Jamie Madill4d0bf552016-12-28 15:45:24 -0500308 if (mInstance)
309 {
310 vkDestroyInstance(mInstance, nullptr);
311 mInstance = VK_NULL_HANDLE;
312 }
313
314 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500315}
316
Jamie Madill21061022018-07-12 23:56:30 -0400317angle::Result RendererVk::initialize(vk::Context *context,
318 const egl::AttributeMap &attribs,
319 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500320{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600321 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
322 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500323 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600324 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500325
Jamie Madill0448ec82016-12-23 13:41:47 -0500326 // Gather global layer properties.
327 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400328 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500329
330 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
331 if (instanceLayerCount > 0)
332 {
Jamie Madill21061022018-07-12 23:56:30 -0400333 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
334 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500335 }
336
Jamie Madille09bd5d2016-11-29 16:20:35 -0500337 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400338 ANGLE_VK_TRY(context,
339 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500340
341 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
342 if (instanceExtensionCount > 0)
343 {
Jamie Madill21061022018-07-12 23:56:30 -0400344 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
345 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500346 }
347
Yuly Novikov199f4292018-01-19 19:04:05 -0500348 const char *const *enabledLayerNames = nullptr;
349 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500350 if (mEnableValidationLayers)
351 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500352 bool layersRequested =
353 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
354 mEnableValidationLayers = GetAvailableValidationLayers(
355 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500356 }
357
Jamie Madille09bd5d2016-11-29 16:20:35 -0500358 std::vector<const char *> enabledInstanceExtensions;
359 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500360 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500361
Jamie Madill0448ec82016-12-23 13:41:47 -0500362 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
363 if (mEnableValidationLayers)
364 {
365 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
366 }
367
Jamie Madille09bd5d2016-11-29 16:20:35 -0500368 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400369 ANGLE_VK_TRY(context,
370 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500371
Jamie Madill327ba852016-11-30 12:38:28 -0500372 VkApplicationInfo applicationInfo;
373 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
374 applicationInfo.pNext = nullptr;
375 applicationInfo.pApplicationName = "ANGLE";
376 applicationInfo.applicationVersion = 1;
377 applicationInfo.pEngineName = "ANGLE";
378 applicationInfo.engineVersion = 1;
379 applicationInfo.apiVersion = VK_API_VERSION_1_0;
380
381 VkInstanceCreateInfo instanceInfo;
382 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
383 instanceInfo.pNext = nullptr;
384 instanceInfo.flags = 0;
385 instanceInfo.pApplicationInfo = &applicationInfo;
386
Jamie Madille09bd5d2016-11-29 16:20:35 -0500387 // Enable requested layers and extensions.
388 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
389 instanceInfo.ppEnabledExtensionNames =
390 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500391 instanceInfo.enabledLayerCount = enabledLayerCount;
392 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500393
Jamie Madill21061022018-07-12 23:56:30 -0400394 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500395
Jamie Madill0448ec82016-12-23 13:41:47 -0500396 if (mEnableValidationLayers)
397 {
398 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
399
400 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
401 debugReportInfo.pNext = nullptr;
402 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
403 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
404 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
405 debugReportInfo.pfnCallback = &DebugReportCallback;
406 debugReportInfo.pUserData = this;
407
408 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
409 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
410 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400411 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
412 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500413 }
414
Jamie Madill4d0bf552016-12-28 15:45:24 -0500415 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400416 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
417 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500418
419 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700420 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400421 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
422 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600423 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700424 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500425
426 // Ensure we can find a graphics queue family.
427 uint32_t queueCount = 0;
428 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
429
Jamie Madill21061022018-07-12 23:56:30 -0400430 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500431
432 mQueueFamilyProperties.resize(queueCount);
433 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
434 mQueueFamilyProperties.data());
435
436 size_t graphicsQueueFamilyCount = false;
437 uint32_t firstGraphicsQueueFamily = 0;
438 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
439 {
440 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
441 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
442 {
443 ASSERT(queueInfo.queueCount > 0);
444 graphicsQueueFamilyCount++;
445 if (firstGraphicsQueueFamily == 0)
446 {
447 firstGraphicsQueueFamily = familyIndex;
448 }
449 break;
450 }
451 }
452
Jamie Madill21061022018-07-12 23:56:30 -0400453 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500454
Jamie Madill12222072018-07-11 14:59:48 -0400455 initFeatures();
456
Jamie Madill4d0bf552016-12-28 15:45:24 -0500457 // If only one queue family, go ahead and initialize the device. If there is more than one
458 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
459 if (graphicsQueueFamilyCount == 1)
460 {
Jamie Madill21061022018-07-12 23:56:30 -0400461 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500462 }
463
Jamie Madill035fd6b2017-10-03 15:43:22 -0400464 // Store the physical device memory properties so we can find the right memory pools.
465 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500466
Jamie Madill06ca6342018-07-12 15:56:53 -0400467 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500468
Jamie Madill6a89d222017-11-02 11:59:51 -0400469 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500470 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
471 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400472
Jamie Madill21061022018-07-12 23:56:30 -0400473 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400474}
475
Jamie Madill21061022018-07-12 23:56:30 -0400476angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500477{
478 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400479 ANGLE_VK_TRY(context,
480 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500481
482 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
483 if (deviceLayerCount > 0)
484 {
Jamie Madill21061022018-07-12 23:56:30 -0400485 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
486 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500487 }
488
489 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400490 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
491 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500492
493 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
494 if (deviceExtensionCount > 0)
495 {
Jamie Madill21061022018-07-12 23:56:30 -0400496 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
497 &deviceExtensionCount,
498 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500499 }
500
Yuly Novikov199f4292018-01-19 19:04:05 -0500501 const char *const *enabledLayerNames = nullptr;
502 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500503 if (mEnableValidationLayers)
504 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500505 mEnableValidationLayers = GetAvailableValidationLayers(
506 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500507 }
508
509 std::vector<const char *> enabledDeviceExtensions;
510 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
511
Luc Ferronbf6dc372018-06-28 15:24:19 -0400512 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
513 if (getFeatures().flipViewportY)
514 {
515 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
516 }
517
Jamie Madill21061022018-07-12 23:56:30 -0400518 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500519
520 VkDeviceQueueCreateInfo queueCreateInfo;
521
522 float zeroPriority = 0.0f;
523
524 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
525 queueCreateInfo.pNext = nullptr;
526 queueCreateInfo.flags = 0;
527 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
528 queueCreateInfo.queueCount = 1;
529 queueCreateInfo.pQueuePriorities = &zeroPriority;
530
531 // Initialize the device
532 VkDeviceCreateInfo createInfo;
533
Jamie Madill50cf2be2018-06-15 09:46:57 -0400534 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
535 createInfo.pNext = nullptr;
536 createInfo.flags = 0;
537 createInfo.queueCreateInfoCount = 1;
538 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500539 createInfo.enabledLayerCount = enabledLayerCount;
540 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500541 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
542 createInfo.ppEnabledExtensionNames =
543 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
544 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
545
Jamie Madill21061022018-07-12 23:56:30 -0400546 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500547
548 mCurrentQueueFamilyIndex = queueFamilyIndex;
549
550 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
551
552 // Initialize the command pool now that we know the queue family index.
553 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500554 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
555 commandPoolInfo.pNext = nullptr;
556 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500557 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
558
Jamie Madill21061022018-07-12 23:56:30 -0400559 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500560
Jamie Madill21061022018-07-12 23:56:30 -0400561 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500562}
563
Jamie Madill21061022018-07-12 23:56:30 -0400564angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
565 VkSurfaceKHR surface,
566 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500567{
568 // We've already initialized a device, and can't re-create it unless it's never been used.
569 // TODO(jmadill): Handle the re-creation case if necessary.
570 if (mDevice != VK_NULL_HANDLE)
571 {
572 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
573
574 // Check if the current device supports present on this surface.
575 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400576 ANGLE_VK_TRY(context,
577 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500578 surface, &supportsPresent));
579
Jamie Madill6cad7732018-07-11 09:01:17 -0400580 if (supportsPresent == VK_TRUE)
581 {
582 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400583 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400584 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585 }
586
587 // Find a graphics and present queue.
588 Optional<uint32_t> newPresentQueue;
589 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
590 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
591 {
592 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
593 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
594 {
595 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400596 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
597 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500598
599 if (supportsPresent == VK_TRUE)
600 {
601 newPresentQueue = queueIndex;
602 break;
603 }
604 }
605 }
606
Jamie Madill21061022018-07-12 23:56:30 -0400607 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
608 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609
Jamie Madill6cad7732018-07-11 09:01:17 -0400610 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400611 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500612}
613
614std::string RendererVk::getVendorString() const
615{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300616 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500617}
618
Jamie Madille09bd5d2016-11-29 16:20:35 -0500619std::string RendererVk::getRendererDescription() const
620{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621 std::stringstream strstr;
622
623 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
624
625 strstr << "Vulkan ";
626 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
627 strstr << VK_VERSION_MINOR(apiVersion) << ".";
628 strstr << VK_VERSION_PATCH(apiVersion);
629
Olli Etuahoc6a06182018-04-13 14:11:46 +0300630 strstr << "(";
631
632 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
633 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
634 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
635 // driver detection.
636 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
637 {
638 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
639 }
640
641 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500642
643 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500644}
645
Jamie Madill12222072018-07-11 14:59:48 -0400646void RendererVk::initFeatures()
647{
648 // Use OpenGL line rasterization rules by default.
649 mFeatures.basicGLLineRasterization = true;
650
Luc Ferronf786b702018-07-10 11:01:43 -0400651 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
652 // investigation. http://anglebug.com/2728
653 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400654}
655
Jamie Madillacccc6c2016-05-03 17:22:10 -0400656void RendererVk::ensureCapsInitialized() const
657{
658 if (!mCapsInitialized)
659 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500660 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400661 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400662 mCapsInitialized = true;
663 }
664}
665
Jamie Madillacccc6c2016-05-03 17:22:10 -0400666const gl::Caps &RendererVk::getNativeCaps() const
667{
668 ensureCapsInitialized();
669 return mNativeCaps;
670}
671
672const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
673{
674 ensureCapsInitialized();
675 return mNativeTextureCaps;
676}
677
678const gl::Extensions &RendererVk::getNativeExtensions() const
679{
680 ensureCapsInitialized();
681 return mNativeExtensions;
682}
683
684const gl::Limitations &RendererVk::getNativeLimitations() const
685{
686 ensureCapsInitialized();
687 return mNativeLimitations;
688}
689
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400690uint32_t RendererVk::getMaxActiveTextures()
691{
692 // TODO(lucferron): expose this limitation to GL in Context Caps
693 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
694 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
695}
696
Jamie Madill49ac74b2017-12-21 14:42:33 -0500697const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500698{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500699 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500700}
701
Jamie Madill21061022018-07-12 23:56:30 -0400702angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500703{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500704 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500705 {
Luc Ferron1617e692018-07-11 11:08:19 -0400706 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
707 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400708
Jamie Madill49ac74b2017-12-21 14:42:33 -0500709 VkSubmitInfo submitInfo;
710 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
711 submitInfo.pNext = nullptr;
712 submitInfo.waitSemaphoreCount = 0;
713 submitInfo.pWaitSemaphores = nullptr;
714 submitInfo.pWaitDstStageMask = nullptr;
715 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400716 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500717 submitInfo.signalSemaphoreCount = 0;
718 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500719
Jamie Madill21061022018-07-12 23:56:30 -0400720 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500721 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500722
Jamie Madill4c26fc22017-02-24 11:04:10 -0500723 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400724 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400725 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400726 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500727}
728
Jamie Madill0c0dc342017-03-24 14:18:51 -0400729void RendererVk::freeAllInFlightResources()
730{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500731 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400732 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500733 batch.fence.destroy(mDevice);
734 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400735 }
736 mInFlightCommands.clear();
737
738 for (auto &garbage : mGarbage)
739 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400740 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400741 }
742 mGarbage.clear();
743}
744
Jamie Madill21061022018-07-12 23:56:30 -0400745angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500746{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500747 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500748
Jamie Madill49ac74b2017-12-21 14:42:33 -0500749 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500750 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500751 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400752 if (result == VK_NOT_READY)
753 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500754
Jamie Madill21061022018-07-12 23:56:30 -0400755 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500756 ASSERT(batch.serial > mLastCompletedQueueSerial);
757 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400758
Jamie Madill49ac74b2017-12-21 14:42:33 -0500759 batch.fence.destroy(mDevice);
760 batch.commandPool.destroy(mDevice);
761 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500762 }
763
Jamie Madill49ac74b2017-12-21 14:42:33 -0500764 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400765
766 size_t freeIndex = 0;
767 for (; freeIndex < mGarbage.size(); ++freeIndex)
768 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500769 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400770 break;
771 }
772
773 // Remove the entries from the garbage list - they should be ready to go.
774 if (freeIndex > 0)
775 {
776 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500777 }
778
Jamie Madill21061022018-07-12 23:56:30 -0400779 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500780}
781
Jamie Madill21061022018-07-12 23:56:30 -0400782angle::Result RendererVk::submitFrame(vk::Context *context,
783 const VkSubmitInfo &submitInfo,
784 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500785{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500786 VkFenceCreateInfo fenceInfo;
787 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
788 fenceInfo.pNext = nullptr;
789 fenceInfo.flags = 0;
790
Jamie Madillbea35a62018-07-05 11:54:10 -0400791 vk::Scoped<CommandBatch> scopedBatch(mDevice);
792 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400793 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500794
Jamie Madill21061022018-07-12 23:56:30 -0400795 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500796
797 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500798 batch.commandPool = std::move(mCommandPool);
799 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500800
Jamie Madillbea35a62018-07-05 11:54:10 -0400801 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400802
803 // Sanity check.
804 ASSERT(mInFlightCommands.size() < 1000u);
805
806 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400807 // TODO(jmadill): Overflow check.
808 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400809
Jamie Madill21061022018-07-12 23:56:30 -0400810 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400811
Jamie Madill49ac74b2017-12-21 14:42:33 -0500812 // Simply null out the command buffer here - it was allocated using the command pool.
813 commandBuffer.releaseHandle();
814
815 // Reallocate the command pool for next frame.
816 // TODO(jmadill): Consider reusing command pools.
817 VkCommandPoolCreateInfo poolInfo;
818 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
819 poolInfo.pNext = nullptr;
820 poolInfo.flags = 0;
821 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
822
Jamie Madill21061022018-07-12 23:56:30 -0400823 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500824
Jamie Madill21061022018-07-12 23:56:30 -0400825 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500826}
827
Jamie Madill4c26fc22017-02-24 11:04:10 -0500828Serial RendererVk::getCurrentQueueSerial() const
829{
830 return mCurrentQueueSerial;
831}
832
Jamie Madillaaca96e2018-06-12 10:19:48 -0400833bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500834{
835 return serial > mLastCompletedQueueSerial;
836}
837
Jamie Madill21061022018-07-12 23:56:30 -0400838angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
839 const vk::RenderPassDesc &desc,
840 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500841{
Jamie Madill21061022018-07-12 23:56:30 -0400842 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500843 renderPassOut);
844}
845
Jamie Madill21061022018-07-12 23:56:30 -0400846angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
847 const vk::RenderPassDesc &desc,
848 const vk::AttachmentOpsArray &ops,
849 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500850{
Jamie Madill21061022018-07-12 23:56:30 -0400851 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500852 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500853}
854
Jamie Madilla5e06072018-05-18 14:36:05 -0400855vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500856{
Jamie Madilla5e06072018-05-18 14:36:05 -0400857 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500858}
859
Jamie Madill21061022018-07-12 23:56:30 -0400860angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500861{
Jamie Madill21061022018-07-12 23:56:30 -0400862 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500863 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500864}
865
Jamie Madill21061022018-07-12 23:56:30 -0400866angle::Result RendererVk::flush(vk::Context *context,
867 const vk::Semaphore &waitSemaphore,
868 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500869{
Jamie Madillbea35a62018-07-05 11:54:10 -0400870 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
871 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500872
873 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
874
875 VkSubmitInfo submitInfo;
876 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
877 submitInfo.pNext = nullptr;
878 submitInfo.waitSemaphoreCount = 1;
879 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
880 submitInfo.pWaitDstStageMask = &waitStageMask;
881 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400882 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500883 submitInfo.signalSemaphoreCount = 1;
884 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
885
Jamie Madill21061022018-07-12 23:56:30 -0400886 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
887 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500888}
889
Jamie Madill78feddc2018-04-27 11:45:05 -0400890Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500891{
Jamie Madill78feddc2018-04-27 11:45:05 -0400892 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500893}
894
Jamie Madill06ca6342018-07-12 15:56:53 -0400895angle::Result RendererVk::getPipeline(vk::Context *context,
896 const vk::ShaderAndSerial &vertexShader,
897 const vk::ShaderAndSerial &fragmentShader,
898 const vk::PipelineLayout &pipelineLayout,
899 const vk::PipelineDesc &pipelineDesc,
900 const gl::AttributesMask &activeAttribLocationsMask,
901 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500902{
Jamie Madill06ca6342018-07-12 15:56:53 -0400903 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400904 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400905 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400906 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
907
908 // Pull in a compatible RenderPass.
909 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400910 ANGLE_TRY(
911 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400912
Jamie Madill21061022018-07-12 23:56:30 -0400913 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400914 activeAttribLocationsMask, vertexShader.get(),
915 fragmentShader.get(), pipelineDesc, pipelineOut);
916}
917
Jamie Madill21061022018-07-12 23:56:30 -0400918angle::Result RendererVk::getDescriptorSetLayout(
919 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400920 const vk::DescriptorSetLayoutDesc &desc,
921 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
922{
Jamie Madill21061022018-07-12 23:56:30 -0400923 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400924}
925
Jamie Madill21061022018-07-12 23:56:30 -0400926angle::Result RendererVk::getPipelineLayout(
927 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400928 const vk::PipelineLayoutDesc &desc,
929 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
930 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
931{
Jamie Madill21061022018-07-12 23:56:30 -0400932 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400933 pipelineLayoutOut);
934}
935
Jamie Madilld47044a2018-04-27 11:45:03 -0400936vk::ShaderLibrary *RendererVk::getShaderLibrary()
937{
938 return &mShaderLibrary;
939}
Luc Ferron90968362018-05-04 08:47:22 -0400940
Jamie Madillaaca96e2018-06-12 10:19:48 -0400941uint32_t GetUniformBufferDescriptorCount()
942{
943 return kUniformBufferDescriptorsPerDescriptorSet;
944}
945
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400946} // namespace rx