blob: 80f41e015d03e3b8c6a0612e3601a103f73b6c4a [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 Madill49ac74b2017-12-21 14:42:33 -050020#include "libANGLE/renderer/vulkan/CommandBufferNode.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
31namespace rx
32{
33
Jamie Madille09bd5d2016-11-29 16:20:35 -050034namespace
35{
36
37VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
38 const std::vector<const char *> &enabledExtensionNames)
39{
40 // Compile the extensions names into a set.
41 std::set<std::string> extensionNames;
42 for (const auto &extensionProp : extensionProps)
43 {
44 extensionNames.insert(extensionProp.extensionName);
45 }
46
Jamie Madillacf2f3a2017-11-21 19:22:44 -050047 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050048 {
49 if (extensionNames.count(extensionName) == 0)
50 {
51 return VK_ERROR_EXTENSION_NOT_PRESENT;
52 }
53 }
54
55 return VK_SUCCESS;
56}
57
Yuly Novikov199f4292018-01-19 19:04:05 -050058VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
59 VkDebugReportObjectTypeEXT objectType,
60 uint64_t object,
61 size_t location,
62 int32_t messageCode,
63 const char *layerPrefix,
64 const char *message,
65 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050066{
67 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
68 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050069 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050070#if !defined(NDEBUG)
71 // Abort the call in Debug builds.
72 return VK_TRUE;
73#endif
74 }
75 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
76 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050077 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050078 }
79 else
80 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050081 // Uncomment this if you want Vulkan spam.
82 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050083 }
84
85 return VK_FALSE;
86}
87
Yuly Novikov199f4292018-01-19 19:04:05 -050088// If we're loading the validation layers, we could be running from any random directory.
89// Change to the executable directory so we can find the layers, then change back to the
90// previous directory to be safe we don't disrupt the application.
91class ScopedVkLoaderEnvironment : angle::NonCopyable
92{
93 public:
94 ScopedVkLoaderEnvironment(bool enableValidationLayers)
95 : mEnableValidationLayers(enableValidationLayers), mChangedCWD(false)
96 {
97// Changing CWD and setting environment variables makes no sense on Android,
98// since this code is a part of Java application there.
99// Android Vulkan loader doesn't need this either.
100#if !defined(ANGLE_PLATFORM_ANDROID)
101 if (mEnableValidationLayers)
102 {
103 const auto &cwd = angle::GetCWD();
104 if (!cwd.valid())
105 {
106 ERR() << "Error getting CWD for Vulkan layers init.";
107 mEnableValidationLayers = false;
108 }
109 else
110 {
111 mPreviousCWD = cwd.value();
112 const char *exeDir = angle::GetExecutableDirectory();
113 mChangedCWD = angle::SetCWD(exeDir);
114 if (!mChangedCWD)
115 {
116 ERR() << "Error setting CWD for Vulkan layers init.";
117 mEnableValidationLayers = false;
118 }
119 }
120 }
121
122 // Override environment variable to use the ANGLE layers.
123 if (mEnableValidationLayers)
124 {
jchen10046fa0e2018-02-02 14:51:36 +0800125 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500126 {
127 ERR() << "Error setting environment for Vulkan layers init.";
128 mEnableValidationLayers = false;
129 }
130 }
131#endif // !defined(ANGLE_PLATFORM_ANDROID)
132 }
133
134 ~ScopedVkLoaderEnvironment()
135 {
136 if (mChangedCWD)
137 {
138#if !defined(ANGLE_PLATFORM_ANDROID)
139 ASSERT(mPreviousCWD.valid());
140 angle::SetCWD(mPreviousCWD.value().c_str());
141#endif // !defined(ANGLE_PLATFORM_ANDROID)
142 }
143 }
144
145 bool canEnableValidationLayers() { return mEnableValidationLayers; }
146
147 private:
148 bool mEnableValidationLayers;
149 bool mChangedCWD;
150 Optional<std::string> mPreviousCWD;
151};
152
Jamie Madille09bd5d2016-11-29 16:20:35 -0500153} // anonymous namespace
154
Jamie Madill49ac74b2017-12-21 14:42:33 -0500155// CommandBatch implementation.
156RendererVk::CommandBatch::CommandBatch()
157{
158}
159
160RendererVk::CommandBatch::~CommandBatch()
161{
162}
163
164RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
165 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
166{
167}
168
169RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
170{
171 std::swap(commandPool, other.commandPool);
172 std::swap(fence, other.fence);
173 std::swap(serial, other.serial);
174 return *this;
175}
176
Jamie Madill9f2a8612017-11-30 12:43:09 -0500177// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500178RendererVk::RendererVk()
179 : mCapsInitialized(false),
180 mInstance(VK_NULL_HANDLE),
181 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500182 mDebugReportCallback(VK_NULL_HANDLE),
183 mPhysicalDevice(VK_NULL_HANDLE),
184 mQueue(VK_NULL_HANDLE),
185 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
186 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500187 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400188 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
189 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500190 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400191{
192}
193
194RendererVk::~RendererVk()
195{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500196 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500197 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500198 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
199 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500200 if (error.isError())
201 {
202 ERR() << "Error during VK shutdown: " << error;
203 }
204 }
205
Jamie Madill8c3988c2017-12-21 14:44:56 -0500206 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
207 {
208 descriptorSetLayout.destroy(mDevice);
209 }
210
211 mGraphicsPipelineLayout.destroy(mDevice);
212
Jamie Madill9f2a8612017-11-30 12:43:09 -0500213 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500214 mPipelineCache.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500215
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500216 if (mGlslangWrapper)
217 {
218 GlslangWrapper::ReleaseReference();
219 mGlslangWrapper = nullptr;
220 }
221
Jamie Madill5deea722017-02-16 10:44:46 -0500222 if (mCommandPool.valid())
223 {
224 mCommandPool.destroy(mDevice);
225 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500226
227 if (mDevice)
228 {
229 vkDestroyDevice(mDevice, nullptr);
230 mDevice = VK_NULL_HANDLE;
231 }
232
Jamie Madill0448ec82016-12-23 13:41:47 -0500233 if (mDebugReportCallback)
234 {
235 ASSERT(mInstance);
236 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
237 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
238 ASSERT(destroyDebugReportCallback);
239 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
240 }
241
Jamie Madill4d0bf552016-12-28 15:45:24 -0500242 if (mInstance)
243 {
244 vkDestroyInstance(mInstance, nullptr);
245 mInstance = VK_NULL_HANDLE;
246 }
247
248 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500249}
250
Frank Henigman29f148b2016-11-23 21:05:36 -0500251vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500252{
Jamie Madilldf9ad2b2018-02-02 12:40:01 -0500253 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500254 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madilla66779f2017-01-06 10:43:44 -0500255
Jamie Madill0448ec82016-12-23 13:41:47 -0500256 // Gather global layer properties.
257 uint32_t instanceLayerCount = 0;
258 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
259
260 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
261 if (instanceLayerCount > 0)
262 {
263 ANGLE_VK_TRY(
264 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
265 }
266
Jamie Madille09bd5d2016-11-29 16:20:35 -0500267 uint32_t instanceExtensionCount = 0;
268 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
269
270 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
271 if (instanceExtensionCount > 0)
272 {
273 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
274 instanceExtensionProps.data()));
275 }
276
Yuly Novikov199f4292018-01-19 19:04:05 -0500277 const char *const *enabledLayerNames = nullptr;
278 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500279 if (mEnableValidationLayers)
280 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500281 bool layersRequested =
282 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
283 mEnableValidationLayers = GetAvailableValidationLayers(
284 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500285 }
286
Jamie Madille09bd5d2016-11-29 16:20:35 -0500287 std::vector<const char *> enabledInstanceExtensions;
288 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500289 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500290
Jamie Madill0448ec82016-12-23 13:41:47 -0500291 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
292 if (mEnableValidationLayers)
293 {
294 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
295 }
296
Jamie Madille09bd5d2016-11-29 16:20:35 -0500297 // Verify the required extensions are in the extension names set. Fail if not.
298 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
299
Jamie Madill327ba852016-11-30 12:38:28 -0500300 VkApplicationInfo applicationInfo;
301 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
302 applicationInfo.pNext = nullptr;
303 applicationInfo.pApplicationName = "ANGLE";
304 applicationInfo.applicationVersion = 1;
305 applicationInfo.pEngineName = "ANGLE";
306 applicationInfo.engineVersion = 1;
307 applicationInfo.apiVersion = VK_API_VERSION_1_0;
308
309 VkInstanceCreateInfo instanceInfo;
310 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
311 instanceInfo.pNext = nullptr;
312 instanceInfo.flags = 0;
313 instanceInfo.pApplicationInfo = &applicationInfo;
314
Jamie Madille09bd5d2016-11-29 16:20:35 -0500315 // Enable requested layers and extensions.
316 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
317 instanceInfo.ppEnabledExtensionNames =
318 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500319 instanceInfo.enabledLayerCount = enabledLayerCount;
320 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500321
322 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
323
Jamie Madill0448ec82016-12-23 13:41:47 -0500324 if (mEnableValidationLayers)
325 {
326 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
327
328 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
329 debugReportInfo.pNext = nullptr;
330 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
331 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
332 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
333 debugReportInfo.pfnCallback = &DebugReportCallback;
334 debugReportInfo.pUserData = this;
335
336 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
337 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
338 ASSERT(createDebugReportCallback);
339 ANGLE_VK_TRY(
340 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
341 }
342
Jamie Madill4d0bf552016-12-28 15:45:24 -0500343 uint32_t physicalDeviceCount = 0;
344 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
345 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
346
347 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
348 physicalDeviceCount = 1;
349 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
350
351 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
352
353 // Ensure we can find a graphics queue family.
354 uint32_t queueCount = 0;
355 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
356
357 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
358
359 mQueueFamilyProperties.resize(queueCount);
360 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
361 mQueueFamilyProperties.data());
362
363 size_t graphicsQueueFamilyCount = false;
364 uint32_t firstGraphicsQueueFamily = 0;
365 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
366 {
367 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
368 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
369 {
370 ASSERT(queueInfo.queueCount > 0);
371 graphicsQueueFamilyCount++;
372 if (firstGraphicsQueueFamily == 0)
373 {
374 firstGraphicsQueueFamily = familyIndex;
375 }
376 break;
377 }
378 }
379
380 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
381
382 // If only one queue family, go ahead and initialize the device. If there is more than one
383 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
384 if (graphicsQueueFamilyCount == 1)
385 {
386 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
387 }
388
Jamie Madill035fd6b2017-10-03 15:43:22 -0400389 // Store the physical device memory properties so we can find the right memory pools.
390 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500391
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500392 mGlslangWrapper = GlslangWrapper::GetReference();
393
Jamie Madill6a89d222017-11-02 11:59:51 -0400394 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500395 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
396 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400397
Jamie Madill8c3988c2017-12-21 14:44:56 -0500398 // Initialize the pipeline layout for GL programs.
399 ANGLE_TRY(initGraphicsPipelineLayout());
400
Jamie Madill327ba852016-11-30 12:38:28 -0500401 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400402}
403
Jamie Madill4d0bf552016-12-28 15:45:24 -0500404vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
405{
406 uint32_t deviceLayerCount = 0;
407 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
408
409 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
410 if (deviceLayerCount > 0)
411 {
412 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
413 deviceLayerProps.data()));
414 }
415
416 uint32_t deviceExtensionCount = 0;
417 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
418 &deviceExtensionCount, nullptr));
419
420 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
421 if (deviceExtensionCount > 0)
422 {
423 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
424 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
425 }
426
Yuly Novikov199f4292018-01-19 19:04:05 -0500427 const char *const *enabledLayerNames = nullptr;
428 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500429 if (mEnableValidationLayers)
430 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500431 mEnableValidationLayers = GetAvailableValidationLayers(
432 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500433 }
434
435 std::vector<const char *> enabledDeviceExtensions;
436 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
437
438 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
439
440 VkDeviceQueueCreateInfo queueCreateInfo;
441
442 float zeroPriority = 0.0f;
443
444 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
445 queueCreateInfo.pNext = nullptr;
446 queueCreateInfo.flags = 0;
447 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
448 queueCreateInfo.queueCount = 1;
449 queueCreateInfo.pQueuePriorities = &zeroPriority;
450
451 // Initialize the device
452 VkDeviceCreateInfo createInfo;
453
454 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
455 createInfo.pNext = nullptr;
456 createInfo.flags = 0;
457 createInfo.queueCreateInfoCount = 1;
458 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500459 createInfo.enabledLayerCount = enabledLayerCount;
460 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500461 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
462 createInfo.ppEnabledExtensionNames =
463 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
464 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
465
466 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
467
468 mCurrentQueueFamilyIndex = queueFamilyIndex;
469
470 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
471
472 // Initialize the command pool now that we know the queue family index.
473 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500474 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
475 commandPoolInfo.pNext = nullptr;
476 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500477 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
478
Jamie Madill5deea722017-02-16 10:44:46 -0500479 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500480
Jamie Madill4d0bf552016-12-28 15:45:24 -0500481 return vk::NoError();
482}
483
484vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
485{
486 // We've already initialized a device, and can't re-create it unless it's never been used.
487 // TODO(jmadill): Handle the re-creation case if necessary.
488 if (mDevice != VK_NULL_HANDLE)
489 {
490 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
491
492 // Check if the current device supports present on this surface.
493 VkBool32 supportsPresent = VK_FALSE;
494 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
495 surface, &supportsPresent));
496
497 return (supportsPresent == VK_TRUE);
498 }
499
500 // Find a graphics and present queue.
501 Optional<uint32_t> newPresentQueue;
502 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
503 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
504 {
505 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
506 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
507 {
508 VkBool32 supportsPresent = VK_FALSE;
509 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
510 &supportsPresent));
511
512 if (supportsPresent == VK_TRUE)
513 {
514 newPresentQueue = queueIndex;
515 break;
516 }
517 }
518 }
519
520 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
521 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
522
523 return newPresentQueue.value();
524}
525
526std::string RendererVk::getVendorString() const
527{
528 switch (mPhysicalDeviceProperties.vendorID)
529 {
530 case VENDOR_ID_AMD:
531 return "Advanced Micro Devices";
532 case VENDOR_ID_NVIDIA:
533 return "NVIDIA";
534 case VENDOR_ID_INTEL:
535 return "Intel";
536 default:
537 {
538 // TODO(jmadill): More vendor IDs.
539 std::stringstream strstr;
540 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
541 return strstr.str();
542 }
543 }
544}
545
Jamie Madille09bd5d2016-11-29 16:20:35 -0500546std::string RendererVk::getRendererDescription() const
547{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500548 std::stringstream strstr;
549
550 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
551
552 strstr << "Vulkan ";
553 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
554 strstr << VK_VERSION_MINOR(apiVersion) << ".";
555 strstr << VK_VERSION_PATCH(apiVersion);
556
557 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
558
559 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500560}
561
Jamie Madillacccc6c2016-05-03 17:22:10 -0400562void RendererVk::ensureCapsInitialized() const
563{
564 if (!mCapsInitialized)
565 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500566 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Luc Ferrone4741fd2018-01-25 13:25:27 -0500567 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400568 mCapsInitialized = true;
569 }
570}
571
Jamie Madillacccc6c2016-05-03 17:22:10 -0400572const gl::Caps &RendererVk::getNativeCaps() const
573{
574 ensureCapsInitialized();
575 return mNativeCaps;
576}
577
578const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
579{
580 ensureCapsInitialized();
581 return mNativeTextureCaps;
582}
583
584const gl::Extensions &RendererVk::getNativeExtensions() const
585{
586 ensureCapsInitialized();
587 return mNativeExtensions;
588}
589
590const gl::Limitations &RendererVk::getNativeLimitations() const
591{
592 ensureCapsInitialized();
593 return mNativeLimitations;
594}
595
Jamie Madill49ac74b2017-12-21 14:42:33 -0500596const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500597{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500598 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500599}
600
Jamie Madill49ac74b2017-12-21 14:42:33 -0500601vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500602{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500603 if (!mOpenCommandGraph.empty())
604 {
605 vk::CommandBuffer commandBatch;
606 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400607
Jamie Madill49ac74b2017-12-21 14:42:33 -0500608 VkSubmitInfo submitInfo;
609 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
610 submitInfo.pNext = nullptr;
611 submitInfo.waitSemaphoreCount = 0;
612 submitInfo.pWaitSemaphores = nullptr;
613 submitInfo.pWaitDstStageMask = nullptr;
614 submitInfo.commandBufferCount = 1;
615 submitInfo.pCommandBuffers = commandBatch.ptr();
616 submitInfo.signalSemaphoreCount = 0;
617 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500618
Jamie Madill49ac74b2017-12-21 14:42:33 -0500619 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
620 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621
Jamie Madill4c26fc22017-02-24 11:04:10 -0500622 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500623 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400624 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500625 return vk::NoError();
626}
627
Jamie Madill0c0dc342017-03-24 14:18:51 -0400628void RendererVk::freeAllInFlightResources()
629{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500630 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400631 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500632 batch.fence.destroy(mDevice);
633 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400634 }
635 mInFlightCommands.clear();
636
637 for (auto &garbage : mGarbage)
638 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400639 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400640 }
641 mGarbage.clear();
642}
643
Jamie Madill4c26fc22017-02-24 11:04:10 -0500644vk::Error RendererVk::checkInFlightCommands()
645{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500646 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500647
Jamie Madill49ac74b2017-12-21 14:42:33 -0500648 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500649 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500650 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400651 if (result == VK_NOT_READY)
652 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500653
Jamie Madill0c0dc342017-03-24 14:18:51 -0400654 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500655 ASSERT(batch.serial > mLastCompletedQueueSerial);
656 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400657
Jamie Madill49ac74b2017-12-21 14:42:33 -0500658 batch.fence.destroy(mDevice);
659 batch.commandPool.destroy(mDevice);
660 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500661 }
662
Jamie Madill49ac74b2017-12-21 14:42:33 -0500663 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400664
665 size_t freeIndex = 0;
666 for (; freeIndex < mGarbage.size(); ++freeIndex)
667 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500668 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400669 break;
670 }
671
672 // Remove the entries from the garbage list - they should be ready to go.
673 if (freeIndex > 0)
674 {
675 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500676 }
677
Jamie Madill4c26fc22017-02-24 11:04:10 -0500678 return vk::NoError();
679}
680
Jamie Madill49ac74b2017-12-21 14:42:33 -0500681vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500682{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500683 VkFenceCreateInfo fenceInfo;
684 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
685 fenceInfo.pNext = nullptr;
686 fenceInfo.flags = 0;
687
688 CommandBatch batch;
689 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
690
691 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500692
693 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500694 batch.commandPool = std::move(mCommandPool);
695 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500696
Jamie Madill49ac74b2017-12-21 14:42:33 -0500697 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400698
699 // Sanity check.
700 ASSERT(mInFlightCommands.size() < 1000u);
701
702 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400703 // TODO(jmadill): Overflow check.
704 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400705
706 ANGLE_TRY(checkInFlightCommands());
707
Jamie Madill49ac74b2017-12-21 14:42:33 -0500708 // Simply null out the command buffer here - it was allocated using the command pool.
709 commandBuffer.releaseHandle();
710
711 // Reallocate the command pool for next frame.
712 // TODO(jmadill): Consider reusing command pools.
713 VkCommandPoolCreateInfo poolInfo;
714 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
715 poolInfo.pNext = nullptr;
716 poolInfo.flags = 0;
717 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
718
719 mCommandPool.init(mDevice, poolInfo);
720
Jamie Madill4c26fc22017-02-24 11:04:10 -0500721 return vk::NoError();
722}
723
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500724GlslangWrapper *RendererVk::getGlslangWrapper()
725{
726 return mGlslangWrapper;
727}
728
Jamie Madill4c26fc22017-02-24 11:04:10 -0500729Serial RendererVk::getCurrentQueueSerial() const
730{
731 return mCurrentQueueSerial;
732}
733
Jamie Madill97760352017-11-09 13:08:29 -0500734bool RendererVk::isResourceInUse(const ResourceVk &resource)
735{
736 return isSerialInUse(resource.getQueueSerial());
737}
738
739bool RendererVk::isSerialInUse(Serial serial)
740{
741 return serial > mLastCompletedQueueSerial;
742}
743
Jamie Madill9f2a8612017-11-30 12:43:09 -0500744vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
745 vk::RenderPass **renderPassOut)
746{
747 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
748 renderPassOut);
749}
750
Jamie Madillbef918c2017-12-13 13:11:30 -0500751vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
752 const vk::AttachmentOpsArray &ops,
753 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500754{
Jamie Madillbef918c2017-12-13 13:11:30 -0500755 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
756 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500757}
758
Jamie Madill49ac74b2017-12-21 14:42:33 -0500759vk::CommandBufferNode *RendererVk::allocateCommandNode()
760{
761 // TODO(jmadill): Use a pool allocator for the CPU node allocations.
762 vk::CommandBufferNode *newCommands = new vk::CommandBufferNode();
763 mOpenCommandGraph.emplace_back(newCommands);
764 return newCommands;
765}
766
767vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
768{
769 VkCommandBufferAllocateInfo primaryInfo;
770 primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
771 primaryInfo.pNext = nullptr;
772 primaryInfo.commandPool = mCommandPool.getHandle();
773 primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
774 primaryInfo.commandBufferCount = 1;
775
776 ANGLE_TRY(commandBatch->init(mDevice, primaryInfo));
777
778 if (mOpenCommandGraph.empty())
779 {
780 return vk::NoError();
781 }
782
783 std::vector<vk::CommandBufferNode *> nodeStack;
784
785 VkCommandBufferBeginInfo beginInfo;
786 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
787 beginInfo.pNext = nullptr;
788 beginInfo.flags = 0;
789 beginInfo.pInheritanceInfo = nullptr;
790
791 ANGLE_TRY(commandBatch->begin(beginInfo));
792
793 for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph)
794 {
795 // Only process commands that don't have child commands. The others will be pulled in
796 // automatically. Also skip commands that have already been visited.
Jamie Madill0e654542018-02-07 14:50:06 -0500797 if (topLevelNode->hasHappensAfterDependencies() ||
Jamie Madill49ac74b2017-12-21 14:42:33 -0500798 topLevelNode->visitedState() != vk::VisitedState::Unvisited)
799 continue;
800
801 nodeStack.push_back(topLevelNode);
802
803 while (!nodeStack.empty())
804 {
805 vk::CommandBufferNode *node = nodeStack.back();
806
807 switch (node->visitedState())
808 {
809 case vk::VisitedState::Unvisited:
810 node->visitDependencies(&nodeStack);
811 break;
812 case vk::VisitedState::Ready:
813 ANGLE_TRY(node->visitAndExecute(this, commandBatch));
814 nodeStack.pop_back();
815 break;
816 case vk::VisitedState::Visited:
817 nodeStack.pop_back();
818 break;
819 default:
820 UNREACHABLE();
821 break;
822 }
823 }
824 }
825
826 ANGLE_TRY(commandBatch->end());
Jamie Madill97f39b32018-01-05 13:14:29 -0500827 resetCommandGraph();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500828 return vk::NoError();
829}
830
831void RendererVk::resetCommandGraph()
832{
833 // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph.
834 for (vk::CommandBufferNode *node : mOpenCommandGraph)
835 {
836 delete node;
837 }
838 mOpenCommandGraph.clear();
839}
840
841vk::Error RendererVk::flush(const gl::Context *context,
842 const vk::Semaphore &waitSemaphore,
843 const vk::Semaphore &signalSemaphore)
844{
845 vk::CommandBuffer commandBatch;
846 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
847
848 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
849
850 VkSubmitInfo submitInfo;
851 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
852 submitInfo.pNext = nullptr;
853 submitInfo.waitSemaphoreCount = 1;
854 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
855 submitInfo.pWaitDstStageMask = &waitStageMask;
856 submitInfo.commandBufferCount = 1;
857 submitInfo.pCommandBuffers = commandBatch.ptr();
858 submitInfo.signalSemaphoreCount = 1;
859 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
860
861 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
862 return vk::NoError();
863}
864
Jamie Madill8c3988c2017-12-21 14:44:56 -0500865const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
866{
867 return mGraphicsPipelineLayout;
868}
869
870const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
871{
872 return mGraphicsDescriptorSetLayouts;
873}
874
875vk::Error RendererVk::initGraphicsPipelineLayout()
876{
877 ASSERT(!mGraphicsPipelineLayout.valid());
878
879 // Create two descriptor set layouts: one for default uniform info, and one for textures.
880 // Skip one or both if there are no uniforms.
881 VkDescriptorSetLayoutBinding uniformBindings[2];
882 uint32_t blockCount = 0;
883
884 {
885 auto &layoutBinding = uniformBindings[blockCount];
886
887 layoutBinding.binding = blockCount;
888 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
889 layoutBinding.descriptorCount = 1;
890 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
891 layoutBinding.pImmutableSamplers = nullptr;
892
893 blockCount++;
894 }
895
896 {
897 auto &layoutBinding = uniformBindings[blockCount];
898
899 layoutBinding.binding = blockCount;
900 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
901 layoutBinding.descriptorCount = 1;
902 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
903 layoutBinding.pImmutableSamplers = nullptr;
904
905 blockCount++;
906 }
907
908 {
909 VkDescriptorSetLayoutCreateInfo uniformInfo;
910 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
911 uniformInfo.pNext = nullptr;
912 uniformInfo.flags = 0;
913 uniformInfo.bindingCount = blockCount;
914 uniformInfo.pBindings = uniformBindings;
915
916 vk::DescriptorSetLayout uniformLayout;
917 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
918 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
919 }
920
Yuly Novikov37968132018-01-23 18:19:29 -0500921 // TODO(lucferron): expose this limitation to GL in Context Caps
922 std::vector<VkDescriptorSetLayoutBinding> textureBindings(
923 std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
924 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES));
Jamie Madill8c3988c2017-12-21 14:44:56 -0500925
926 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500927 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500928 {
929 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
930
931 layoutBinding.binding = textureIndex;
932 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
933 layoutBinding.descriptorCount = 1;
934 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
935 layoutBinding.pImmutableSamplers = nullptr;
936 }
937
938 {
939 VkDescriptorSetLayoutCreateInfo textureInfo;
940 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
941 textureInfo.pNext = nullptr;
942 textureInfo.flags = 0;
943 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
944 textureInfo.pBindings = textureBindings.data();
945
946 vk::DescriptorSetLayout textureLayout;
947 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
948 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
949 }
950
951 VkPipelineLayoutCreateInfo createInfo;
952 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
953 createInfo.pNext = nullptr;
954 createInfo.flags = 0;
955 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
956 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
957 createInfo.pushConstantRangeCount = 0;
958 createInfo.pPushConstantRanges = nullptr;
959
960 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
961
962 return vk::NoError();
963}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500964
965Serial RendererVk::issueProgramSerial()
966{
967 return mProgramSerialFactory.generate();
968}
969
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500970vk::Error RendererVk::getPipeline(const ProgramVk *programVk,
971 const vk::PipelineDesc &desc,
Luc Ferronceb71902018-02-05 15:18:47 -0500972 const gl::AttributesMask &activeAttribLocationsMask,
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500973 vk::PipelineAndSerial **pipelineOut)
974{
975 ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial);
976 ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial);
977
978 // Pull in a compatible RenderPass.
979 vk::RenderPass *compatibleRenderPass = nullptr;
980 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
981
982 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500983 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500984 programVk->getLinkedFragmentModule(), desc, pipelineOut);
985}
986
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400987} // namespace rx