blob: 175ce651e7f7ea897c427984a6065265a07afa9e [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/TextureVk.h"
26#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
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070031// Consts
32namespace
33{
34const uint32_t kMockVendorID = 0xba5eba11;
35const uint32_t kMockDeviceID = 0xf005ba11;
36constexpr char kMockDeviceName[] = "Vulkan Mock Device";
37} // anonymous namespace
38
Jamie Madill9e54b5a2016-05-25 12:57:39 -040039namespace rx
40{
41
Jamie Madille09bd5d2016-11-29 16:20:35 -050042namespace
43{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040044// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
45// one for the vertex shader.
46constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050047
48VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
49 const std::vector<const char *> &enabledExtensionNames)
50{
51 // Compile the extensions names into a set.
52 std::set<std::string> extensionNames;
53 for (const auto &extensionProp : extensionProps)
54 {
55 extensionNames.insert(extensionProp.extensionName);
56 }
57
Jamie Madillacf2f3a2017-11-21 19:22:44 -050058 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050059 {
60 if (extensionNames.count(extensionName) == 0)
61 {
62 return VK_ERROR_EXTENSION_NOT_PRESENT;
63 }
64 }
65
66 return VK_SUCCESS;
67}
68
Yuly Novikov199f4292018-01-19 19:04:05 -050069VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
70 VkDebugReportObjectTypeEXT objectType,
71 uint64_t object,
72 size_t location,
73 int32_t messageCode,
74 const char *layerPrefix,
75 const char *message,
76 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050077{
78 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
79 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050080 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050081#if !defined(NDEBUG)
82 // Abort the call in Debug builds.
83 return VK_TRUE;
84#endif
85 }
86 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
87 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050088 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050089 }
90 else
91 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050092 // Uncomment this if you want Vulkan spam.
93 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050094 }
95
96 return VK_FALSE;
97}
98
Yuly Novikov199f4292018-01-19 19:04:05 -050099// If we're loading the validation layers, we could be running from any random directory.
100// Change to the executable directory so we can find the layers, then change back to the
101// previous directory to be safe we don't disrupt the application.
102class ScopedVkLoaderEnvironment : angle::NonCopyable
103{
104 public:
105 ScopedVkLoaderEnvironment(bool enableValidationLayers)
106 : mEnableValidationLayers(enableValidationLayers), mChangedCWD(false)
107 {
108// Changing CWD and setting environment variables makes no sense on Android,
109// since this code is a part of Java application there.
110// Android Vulkan loader doesn't need this either.
111#if !defined(ANGLE_PLATFORM_ANDROID)
112 if (mEnableValidationLayers)
113 {
114 const auto &cwd = angle::GetCWD();
115 if (!cwd.valid())
116 {
117 ERR() << "Error getting CWD for Vulkan layers init.";
118 mEnableValidationLayers = false;
119 }
120 else
121 {
122 mPreviousCWD = cwd.value();
123 const char *exeDir = angle::GetExecutableDirectory();
124 mChangedCWD = angle::SetCWD(exeDir);
125 if (!mChangedCWD)
126 {
127 ERR() << "Error setting CWD for Vulkan layers init.";
128 mEnableValidationLayers = false;
129 }
130 }
131 }
132
133 // Override environment variable to use the ANGLE layers.
134 if (mEnableValidationLayers)
135 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700136 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500137 {
138 ERR() << "Error setting environment for Vulkan layers init.";
139 mEnableValidationLayers = false;
140 }
141 }
142#endif // !defined(ANGLE_PLATFORM_ANDROID)
143 }
144
145 ~ScopedVkLoaderEnvironment()
146 {
147 if (mChangedCWD)
148 {
149#if !defined(ANGLE_PLATFORM_ANDROID)
150 ASSERT(mPreviousCWD.valid());
151 angle::SetCWD(mPreviousCWD.value().c_str());
152#endif // !defined(ANGLE_PLATFORM_ANDROID)
153 }
154 }
155
156 bool canEnableValidationLayers() { return mEnableValidationLayers; }
157
158 private:
159 bool mEnableValidationLayers;
160 bool mChangedCWD;
161 Optional<std::string> mPreviousCWD;
162};
163
Jamie Madille09bd5d2016-11-29 16:20:35 -0500164} // anonymous namespace
165
Jamie Madill49ac74b2017-12-21 14:42:33 -0500166// CommandBatch implementation.
167RendererVk::CommandBatch::CommandBatch()
168{
169}
170
171RendererVk::CommandBatch::~CommandBatch()
172{
173}
174
175RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
176 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
177{
178}
179
180RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
181{
182 std::swap(commandPool, other.commandPool);
183 std::swap(fence, other.fence);
184 std::swap(serial, other.serial);
185 return *this;
186}
187
Jamie Madill9f2a8612017-11-30 12:43:09 -0500188// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500189RendererVk::RendererVk()
190 : mCapsInitialized(false),
191 mInstance(VK_NULL_HANDLE),
192 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500193 mDebugReportCallback(VK_NULL_HANDLE),
194 mPhysicalDevice(VK_NULL_HANDLE),
195 mQueue(VK_NULL_HANDLE),
196 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
197 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500198 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400199 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
200 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500201 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400202{
203}
204
205RendererVk::~RendererVk()
206{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500207 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500208 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500209 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
210 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500211 if (error.isError())
212 {
213 ERR() << "Error during VK shutdown: " << error;
214 }
215 }
216
Jamie Madill8c3988c2017-12-21 14:44:56 -0500217 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
218 {
219 descriptorSetLayout.destroy(mDevice);
220 }
221
222 mGraphicsPipelineLayout.destroy(mDevice);
jchen10be7f44f2018-05-21 14:35:32 +0800223 mInternalPushConstantPipelineLayout.destroy(mDevice);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500224
Jamie Madill9f2a8612017-11-30 12:43:09 -0500225 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500226 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400227 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500228
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500229 if (mGlslangWrapper)
230 {
231 GlslangWrapper::ReleaseReference();
232 mGlslangWrapper = nullptr;
233 }
234
Jamie Madill5deea722017-02-16 10:44:46 -0500235 if (mCommandPool.valid())
236 {
237 mCommandPool.destroy(mDevice);
238 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500239
240 if (mDevice)
241 {
242 vkDestroyDevice(mDevice, nullptr);
243 mDevice = VK_NULL_HANDLE;
244 }
245
Jamie Madill0448ec82016-12-23 13:41:47 -0500246 if (mDebugReportCallback)
247 {
248 ASSERT(mInstance);
249 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
250 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
251 ASSERT(destroyDebugReportCallback);
252 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
253 }
254
Jamie Madill4d0bf552016-12-28 15:45:24 -0500255 if (mInstance)
256 {
257 vkDestroyInstance(mInstance, nullptr);
258 mInstance = VK_NULL_HANDLE;
259 }
260
261 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500262}
263
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700264void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
265 bool preferMockICD,
266 VkPhysicalDevice *physicalDeviceOut,
267 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
268{
269 ASSERT(!physicalDevices.empty());
270 if (preferMockICD)
271 {
272 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
273 {
274 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
275 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
276 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
277 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
278 {
279 *physicalDeviceOut = physicalDevice;
280 return;
281 }
282 }
283 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
284 "physicalDevice instead.";
285 }
286
287 // Fall back to first device.
288 *physicalDeviceOut = physicalDevices[0];
289 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
290}
291
Frank Henigman29f148b2016-11-23 21:05:36 -0500292vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500293{
Jamie Madilldf9ad2b2018-02-02 12:40:01 -0500294 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500295 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madilla66779f2017-01-06 10:43:44 -0500296
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700297 bool enableNullDriver = false;
298#if !defined(ANGLE_PLATFORM_ANDROID)
299 // Mock ICD does not currently run on Android
300 enableNullDriver = (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
301 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
302 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
303 if (enableNullDriver)
304 {
305 // Override environment variable to use built Mock ICD
306 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
307 ANGLE_VK_CHECK(angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON),
308 VK_ERROR_INITIALIZATION_FAILED);
309 }
310#endif // !defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill0448ec82016-12-23 13:41:47 -0500311 // Gather global layer properties.
312 uint32_t instanceLayerCount = 0;
313 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
314
315 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
316 if (instanceLayerCount > 0)
317 {
318 ANGLE_VK_TRY(
319 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
320 }
321
Jamie Madille09bd5d2016-11-29 16:20:35 -0500322 uint32_t instanceExtensionCount = 0;
323 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
324
325 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
326 if (instanceExtensionCount > 0)
327 {
328 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
329 instanceExtensionProps.data()));
330 }
331
Yuly Novikov199f4292018-01-19 19:04:05 -0500332 const char *const *enabledLayerNames = nullptr;
333 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500334 if (mEnableValidationLayers)
335 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500336 bool layersRequested =
337 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
338 mEnableValidationLayers = GetAvailableValidationLayers(
339 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500340 }
341
Jamie Madille09bd5d2016-11-29 16:20:35 -0500342 std::vector<const char *> enabledInstanceExtensions;
343 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500344 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500345
Jamie Madill0448ec82016-12-23 13:41:47 -0500346 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
347 if (mEnableValidationLayers)
348 {
349 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
350 }
351
Jamie Madille09bd5d2016-11-29 16:20:35 -0500352 // Verify the required extensions are in the extension names set. Fail if not.
353 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
354
Jamie Madill327ba852016-11-30 12:38:28 -0500355 VkApplicationInfo applicationInfo;
356 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
357 applicationInfo.pNext = nullptr;
358 applicationInfo.pApplicationName = "ANGLE";
359 applicationInfo.applicationVersion = 1;
360 applicationInfo.pEngineName = "ANGLE";
361 applicationInfo.engineVersion = 1;
362 applicationInfo.apiVersion = VK_API_VERSION_1_0;
363
364 VkInstanceCreateInfo instanceInfo;
365 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
366 instanceInfo.pNext = nullptr;
367 instanceInfo.flags = 0;
368 instanceInfo.pApplicationInfo = &applicationInfo;
369
Jamie Madille09bd5d2016-11-29 16:20:35 -0500370 // Enable requested layers and extensions.
371 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
372 instanceInfo.ppEnabledExtensionNames =
373 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500374 instanceInfo.enabledLayerCount = enabledLayerCount;
375 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500376
377 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
378
Jamie Madill0448ec82016-12-23 13:41:47 -0500379 if (mEnableValidationLayers)
380 {
381 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
382
383 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
384 debugReportInfo.pNext = nullptr;
385 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
386 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
387 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
388 debugReportInfo.pfnCallback = &DebugReportCallback;
389 debugReportInfo.pUserData = this;
390
391 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
392 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
393 ASSERT(createDebugReportCallback);
394 ANGLE_VK_TRY(
395 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
396 }
397
Jamie Madill4d0bf552016-12-28 15:45:24 -0500398 uint32_t physicalDeviceCount = 0;
399 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
400 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
401
402 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700403 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
404 ANGLE_VK_TRY(
405 vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, physicalDevices.data()));
406 ChoosePhysicalDevice(physicalDevices, enableNullDriver, &mPhysicalDevice,
407 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500408
409 // Ensure we can find a graphics queue family.
410 uint32_t queueCount = 0;
411 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
412
413 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
414
415 mQueueFamilyProperties.resize(queueCount);
416 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
417 mQueueFamilyProperties.data());
418
419 size_t graphicsQueueFamilyCount = false;
420 uint32_t firstGraphicsQueueFamily = 0;
421 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
422 {
423 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
424 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
425 {
426 ASSERT(queueInfo.queueCount > 0);
427 graphicsQueueFamilyCount++;
428 if (firstGraphicsQueueFamily == 0)
429 {
430 firstGraphicsQueueFamily = familyIndex;
431 }
432 break;
433 }
434 }
435
436 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
437
438 // If only one queue family, go ahead and initialize the device. If there is more than one
439 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
440 if (graphicsQueueFamilyCount == 1)
441 {
442 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
443 }
444
Jamie Madill035fd6b2017-10-03 15:43:22 -0400445 // Store the physical device memory properties so we can find the right memory pools.
446 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500447
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500448 mGlslangWrapper = GlslangWrapper::GetReference();
449
Jamie Madill6a89d222017-11-02 11:59:51 -0400450 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500451 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
452 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400453
Jamie Madill8c3988c2017-12-21 14:44:56 -0500454 // Initialize the pipeline layout for GL programs.
455 ANGLE_TRY(initGraphicsPipelineLayout());
456
Jamie Madill327ba852016-11-30 12:38:28 -0500457 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400458}
459
Jamie Madill4d0bf552016-12-28 15:45:24 -0500460vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
461{
462 uint32_t deviceLayerCount = 0;
463 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
464
465 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
466 if (deviceLayerCount > 0)
467 {
468 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
469 deviceLayerProps.data()));
470 }
471
472 uint32_t deviceExtensionCount = 0;
473 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
474 &deviceExtensionCount, nullptr));
475
476 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
477 if (deviceExtensionCount > 0)
478 {
479 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
480 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
481 }
482
Yuly Novikov199f4292018-01-19 19:04:05 -0500483 const char *const *enabledLayerNames = nullptr;
484 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500485 if (mEnableValidationLayers)
486 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500487 mEnableValidationLayers = GetAvailableValidationLayers(
488 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500489 }
490
491 std::vector<const char *> enabledDeviceExtensions;
492 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
493
494 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
495
496 VkDeviceQueueCreateInfo queueCreateInfo;
497
498 float zeroPriority = 0.0f;
499
500 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
501 queueCreateInfo.pNext = nullptr;
502 queueCreateInfo.flags = 0;
503 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
504 queueCreateInfo.queueCount = 1;
505 queueCreateInfo.pQueuePriorities = &zeroPriority;
506
507 // Initialize the device
508 VkDeviceCreateInfo createInfo;
509
510 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
511 createInfo.pNext = nullptr;
512 createInfo.flags = 0;
513 createInfo.queueCreateInfoCount = 1;
514 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500515 createInfo.enabledLayerCount = enabledLayerCount;
516 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500517 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
518 createInfo.ppEnabledExtensionNames =
519 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
520 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
521
522 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
523
524 mCurrentQueueFamilyIndex = queueFamilyIndex;
525
526 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
527
528 // Initialize the command pool now that we know the queue family index.
529 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500530 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
531 commandPoolInfo.pNext = nullptr;
532 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500533 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
534
Jamie Madill5deea722017-02-16 10:44:46 -0500535 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500536
Jamie Madill4d0bf552016-12-28 15:45:24 -0500537 return vk::NoError();
538}
539
540vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
541{
542 // We've already initialized a device, and can't re-create it unless it's never been used.
543 // TODO(jmadill): Handle the re-creation case if necessary.
544 if (mDevice != VK_NULL_HANDLE)
545 {
546 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
547
548 // Check if the current device supports present on this surface.
549 VkBool32 supportsPresent = VK_FALSE;
550 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
551 surface, &supportsPresent));
552
553 return (supportsPresent == VK_TRUE);
554 }
555
556 // Find a graphics and present queue.
557 Optional<uint32_t> newPresentQueue;
558 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
559 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
560 {
561 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
562 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
563 {
564 VkBool32 supportsPresent = VK_FALSE;
565 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
566 &supportsPresent));
567
568 if (supportsPresent == VK_TRUE)
569 {
570 newPresentQueue = queueIndex;
571 break;
572 }
573 }
574 }
575
576 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
577 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
578
579 return newPresentQueue.value();
580}
581
582std::string RendererVk::getVendorString() const
583{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300584 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585}
586
Jamie Madille09bd5d2016-11-29 16:20:35 -0500587std::string RendererVk::getRendererDescription() const
588{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500589 std::stringstream strstr;
590
591 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
592
593 strstr << "Vulkan ";
594 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
595 strstr << VK_VERSION_MINOR(apiVersion) << ".";
596 strstr << VK_VERSION_PATCH(apiVersion);
597
Olli Etuahoc6a06182018-04-13 14:11:46 +0300598 strstr << "(";
599
600 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
601 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
602 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
603 // driver detection.
604 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
605 {
606 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
607 }
608
609 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500610
611 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500612}
613
Jamie Madillacccc6c2016-05-03 17:22:10 -0400614void RendererVk::ensureCapsInitialized() const
615{
616 if (!mCapsInitialized)
617 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500618 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Luc Ferrone4741fd2018-01-25 13:25:27 -0500619 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400620 mCapsInitialized = true;
621 }
622}
623
Jamie Madillacccc6c2016-05-03 17:22:10 -0400624const gl::Caps &RendererVk::getNativeCaps() const
625{
626 ensureCapsInitialized();
627 return mNativeCaps;
628}
629
630const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
631{
632 ensureCapsInitialized();
633 return mNativeTextureCaps;
634}
635
636const gl::Extensions &RendererVk::getNativeExtensions() const
637{
638 ensureCapsInitialized();
639 return mNativeExtensions;
640}
641
642const gl::Limitations &RendererVk::getNativeLimitations() const
643{
644 ensureCapsInitialized();
645 return mNativeLimitations;
646}
647
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400648uint32_t RendererVk::getMaxActiveTextures()
649{
650 // TODO(lucferron): expose this limitation to GL in Context Caps
651 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
652 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
653}
654
655uint32_t RendererVk::getUniformBufferDescriptorCount()
656{
657 return kUniformBufferDescriptorsPerDescriptorSet;
658}
659
Jamie Madill49ac74b2017-12-21 14:42:33 -0500660const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500661{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500662 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500663}
664
Jamie Madill49ac74b2017-12-21 14:42:33 -0500665vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500666{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500667 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500668 {
669 vk::CommandBuffer commandBatch;
670 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400671
Jamie Madill49ac74b2017-12-21 14:42:33 -0500672 VkSubmitInfo submitInfo;
673 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
674 submitInfo.pNext = nullptr;
675 submitInfo.waitSemaphoreCount = 0;
676 submitInfo.pWaitSemaphores = nullptr;
677 submitInfo.pWaitDstStageMask = nullptr;
678 submitInfo.commandBufferCount = 1;
679 submitInfo.pCommandBuffers = commandBatch.ptr();
680 submitInfo.signalSemaphoreCount = 0;
681 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500682
Jamie Madill49ac74b2017-12-21 14:42:33 -0500683 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
684 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500685
Jamie Madill4c26fc22017-02-24 11:04:10 -0500686 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500687 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400688 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500689 return vk::NoError();
690}
691
Jamie Madill0c0dc342017-03-24 14:18:51 -0400692void RendererVk::freeAllInFlightResources()
693{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500694 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400695 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500696 batch.fence.destroy(mDevice);
697 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400698 }
699 mInFlightCommands.clear();
700
701 for (auto &garbage : mGarbage)
702 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400703 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400704 }
705 mGarbage.clear();
706}
707
Jamie Madill4c26fc22017-02-24 11:04:10 -0500708vk::Error RendererVk::checkInFlightCommands()
709{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500710 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500711
Jamie Madill49ac74b2017-12-21 14:42:33 -0500712 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500713 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500714 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400715 if (result == VK_NOT_READY)
716 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500717
Jamie Madill0c0dc342017-03-24 14:18:51 -0400718 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500719 ASSERT(batch.serial > mLastCompletedQueueSerial);
720 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400721
Jamie Madill49ac74b2017-12-21 14:42:33 -0500722 batch.fence.destroy(mDevice);
723 batch.commandPool.destroy(mDevice);
724 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500725 }
726
Jamie Madill49ac74b2017-12-21 14:42:33 -0500727 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400728
729 size_t freeIndex = 0;
730 for (; freeIndex < mGarbage.size(); ++freeIndex)
731 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500732 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400733 break;
734 }
735
736 // Remove the entries from the garbage list - they should be ready to go.
737 if (freeIndex > 0)
738 {
739 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500740 }
741
Jamie Madill4c26fc22017-02-24 11:04:10 -0500742 return vk::NoError();
743}
744
Jamie Madill49ac74b2017-12-21 14:42:33 -0500745vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500746{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500747 VkFenceCreateInfo fenceInfo;
748 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
749 fenceInfo.pNext = nullptr;
750 fenceInfo.flags = 0;
751
752 CommandBatch batch;
753 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
754
755 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500756
757 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500758 batch.commandPool = std::move(mCommandPool);
759 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500760
Jamie Madill49ac74b2017-12-21 14:42:33 -0500761 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400762
763 // Sanity check.
764 ASSERT(mInFlightCommands.size() < 1000u);
765
766 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400767 // TODO(jmadill): Overflow check.
768 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400769
770 ANGLE_TRY(checkInFlightCommands());
771
Jamie Madill49ac74b2017-12-21 14:42:33 -0500772 // Simply null out the command buffer here - it was allocated using the command pool.
773 commandBuffer.releaseHandle();
774
775 // Reallocate the command pool for next frame.
776 // TODO(jmadill): Consider reusing command pools.
777 VkCommandPoolCreateInfo poolInfo;
778 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
779 poolInfo.pNext = nullptr;
780 poolInfo.flags = 0;
781 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
782
783 mCommandPool.init(mDevice, poolInfo);
784
Jamie Madill4c26fc22017-02-24 11:04:10 -0500785 return vk::NoError();
786}
787
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500788GlslangWrapper *RendererVk::getGlslangWrapper()
789{
790 return mGlslangWrapper;
791}
792
Jamie Madill4c26fc22017-02-24 11:04:10 -0500793Serial RendererVk::getCurrentQueueSerial() const
794{
795 return mCurrentQueueSerial;
796}
797
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400798bool RendererVk::isResourceInUse(const vk::CommandGraphResource &resource)
Jamie Madill97760352017-11-09 13:08:29 -0500799{
800 return isSerialInUse(resource.getQueueSerial());
801}
802
803bool RendererVk::isSerialInUse(Serial serial)
804{
805 return serial > mLastCompletedQueueSerial;
806}
807
Jamie Madill9f2a8612017-11-30 12:43:09 -0500808vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
809 vk::RenderPass **renderPassOut)
810{
811 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
812 renderPassOut);
813}
814
Jamie Madillbef918c2017-12-13 13:11:30 -0500815vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
816 const vk::AttachmentOpsArray &ops,
817 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500818{
Jamie Madillbef918c2017-12-13 13:11:30 -0500819 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
820 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500821}
822
Jamie Madill1f46bc12018-02-20 16:09:43 -0500823vk::CommandGraphNode *RendererVk::allocateCommandNode()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500824{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500825 return mCommandGraph.allocateNode();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500826}
827
828vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
829{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500830 return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache,
831 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500832}
833
834vk::Error RendererVk::flush(const gl::Context *context,
835 const vk::Semaphore &waitSemaphore,
836 const vk::Semaphore &signalSemaphore)
837{
838 vk::CommandBuffer commandBatch;
839 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
840
841 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
842
843 VkSubmitInfo submitInfo;
844 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
845 submitInfo.pNext = nullptr;
846 submitInfo.waitSemaphoreCount = 1;
847 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
848 submitInfo.pWaitDstStageMask = &waitStageMask;
849 submitInfo.commandBufferCount = 1;
850 submitInfo.pCommandBuffers = commandBatch.ptr();
851 submitInfo.signalSemaphoreCount = 1;
852 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
853
854 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
855 return vk::NoError();
856}
857
Jamie Madill8c3988c2017-12-21 14:44:56 -0500858const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
859{
860 return mGraphicsPipelineLayout;
861}
862
863const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
864{
865 return mGraphicsDescriptorSetLayouts;
866}
867
868vk::Error RendererVk::initGraphicsPipelineLayout()
869{
870 ASSERT(!mGraphicsPipelineLayout.valid());
871
872 // Create two descriptor set layouts: one for default uniform info, and one for textures.
873 // Skip one or both if there are no uniforms.
874 VkDescriptorSetLayoutBinding uniformBindings[2];
875 uint32_t blockCount = 0;
876
877 {
Jamie Madill9aef3672018-04-27 11:45:06 -0400878 VkDescriptorSetLayoutBinding &layoutBinding = uniformBindings[blockCount];
Jamie Madill8c3988c2017-12-21 14:44:56 -0500879
880 layoutBinding.binding = blockCount;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400881 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500882 layoutBinding.descriptorCount = 1;
883 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
884 layoutBinding.pImmutableSamplers = nullptr;
885
886 blockCount++;
887 }
888
889 {
Jamie Madill9aef3672018-04-27 11:45:06 -0400890 VkDescriptorSetLayoutBinding &layoutBinding = uniformBindings[blockCount];
Jamie Madill8c3988c2017-12-21 14:44:56 -0500891
892 layoutBinding.binding = blockCount;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400893 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500894 layoutBinding.descriptorCount = 1;
895 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
896 layoutBinding.pImmutableSamplers = nullptr;
897
898 blockCount++;
899 }
900
901 {
902 VkDescriptorSetLayoutCreateInfo uniformInfo;
903 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
904 uniformInfo.pNext = nullptr;
905 uniformInfo.flags = 0;
906 uniformInfo.bindingCount = blockCount;
907 uniformInfo.pBindings = uniformBindings;
908
909 vk::DescriptorSetLayout uniformLayout;
910 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
911 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
912 }
913
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400914 std::vector<VkDescriptorSetLayoutBinding> textureBindings(getMaxActiveTextures());
Jamie Madill8c3988c2017-12-21 14:44:56 -0500915
916 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500917 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500918 {
919 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
920
921 layoutBinding.binding = textureIndex;
922 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
923 layoutBinding.descriptorCount = 1;
924 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
925 layoutBinding.pImmutableSamplers = nullptr;
926 }
927
928 {
929 VkDescriptorSetLayoutCreateInfo textureInfo;
930 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
931 textureInfo.pNext = nullptr;
932 textureInfo.flags = 0;
933 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
934 textureInfo.pBindings = textureBindings.data();
935
936 vk::DescriptorSetLayout textureLayout;
937 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
938 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
939 }
940
941 VkPipelineLayoutCreateInfo createInfo;
942 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
943 createInfo.pNext = nullptr;
944 createInfo.flags = 0;
945 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
946 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
947 createInfo.pushConstantRangeCount = 0;
948 createInfo.pPushConstantRanges = nullptr;
949
950 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
951
952 return vk::NoError();
953}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500954
jchen10be7f44f2018-05-21 14:35:32 +0800955vk::Error RendererVk::getInternalPushConstantPipelineLayout(
956 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill9aef3672018-04-27 11:45:06 -0400957{
jchen10be7f44f2018-05-21 14:35:32 +0800958 *pipelineLayoutOut = &mInternalPushConstantPipelineLayout;
959 if (mInternalPushConstantPipelineLayout.valid())
Jamie Madill9aef3672018-04-27 11:45:06 -0400960 {
961 return vk::NoError();
962 }
963
jchen10be7f44f2018-05-21 14:35:32 +0800964 VkPushConstantRange pushConstantRange;
965 pushConstantRange.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
966 pushConstantRange.offset = 0;
967 pushConstantRange.size = sizeof(VkClearColorValue);
968
Jamie Madill9aef3672018-04-27 11:45:06 -0400969 VkPipelineLayoutCreateInfo createInfo;
970 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
971 createInfo.pNext = nullptr;
972 createInfo.flags = 0;
jchen10be7f44f2018-05-21 14:35:32 +0800973 createInfo.setLayoutCount = 0;
974 createInfo.pSetLayouts = nullptr;
975 createInfo.pushConstantRangeCount = 1;
976 createInfo.pPushConstantRanges = &pushConstantRange;
Jamie Madill9aef3672018-04-27 11:45:06 -0400977
jchen10be7f44f2018-05-21 14:35:32 +0800978 ANGLE_TRY(mInternalPushConstantPipelineLayout.init(mDevice, createInfo));
Jamie Madill9aef3672018-04-27 11:45:06 -0400979
980 return vk::NoError();
981}
982
Jamie Madill78feddc2018-04-27 11:45:05 -0400983Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500984{
Jamie Madill78feddc2018-04-27 11:45:05 -0400985 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500986}
987
Jamie Madill9aef3672018-04-27 11:45:06 -0400988vk::Error RendererVk::getAppPipeline(const ProgramVk *programVk,
989 const vk::PipelineDesc &desc,
990 const gl::AttributesMask &activeAttribLocationsMask,
991 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500992{
Jamie Madill33318de2018-05-01 11:22:54 -0400993 ASSERT(programVk->getVertexModuleSerial() ==
994 desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
995 ASSERT(programVk->getFragmentModuleSerial() ==
996 desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500997
998 // Pull in a compatible RenderPass.
999 vk::RenderPass *compatibleRenderPass = nullptr;
1000 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
1001
1002 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -05001003 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001004 programVk->getLinkedFragmentModule(), desc, pipelineOut);
1005}
1006
Jamie Madill9aef3672018-04-27 11:45:06 -04001007vk::Error RendererVk::getInternalPipeline(const vk::ShaderAndSerial &vertexShader,
1008 const vk::ShaderAndSerial &fragmentShader,
1009 const vk::PipelineLayout &pipelineLayout,
1010 const vk::PipelineDesc &pipelineDesc,
1011 const gl::AttributesMask &activeAttribLocationsMask,
1012 vk::PipelineAndSerial **pipelineOut)
1013{
1014 ASSERT(vertexShader.queueSerial() ==
1015 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
1016 ASSERT(fragmentShader.queueSerial() ==
1017 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1018
1019 // Pull in a compatible RenderPass.
1020 vk::RenderPass *compatibleRenderPass = nullptr;
1021 ANGLE_TRY(getCompatibleRenderPass(pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
1022
1023 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
1024 activeAttribLocationsMask, vertexShader.get(),
1025 fragmentShader.get(), pipelineDesc, pipelineOut);
1026}
1027
Jamie Madilld47044a2018-04-27 11:45:03 -04001028vk::ShaderLibrary *RendererVk::getShaderLibrary()
1029{
1030 return &mShaderLibrary;
1031}
Luc Ferron90968362018-05-04 08:47:22 -04001032
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001033} // namespace rx