blob: 71f2bef63c07675f22d9a3a3e7a5eb2d5236714f [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
Luc Ferrone4741fd2018-01-25 13:25:27 -050058VkBool32 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
Jamie Madille09bd5d2016-11-29 16:20:35 -050088} // anonymous namespace
89
Jamie Madill49ac74b2017-12-21 14:42:33 -050090// CommandBatch implementation.
91RendererVk::CommandBatch::CommandBatch()
92{
93}
94
95RendererVk::CommandBatch::~CommandBatch()
96{
97}
98
99RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
100 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
101{
102}
103
104RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
105{
106 std::swap(commandPool, other.commandPool);
107 std::swap(fence, other.fence);
108 std::swap(serial, other.serial);
109 return *this;
110}
111
Jamie Madill9f2a8612017-11-30 12:43:09 -0500112// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500113RendererVk::RendererVk()
114 : mCapsInitialized(false),
115 mInstance(VK_NULL_HANDLE),
116 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500117 mDebugReportCallback(VK_NULL_HANDLE),
118 mPhysicalDevice(VK_NULL_HANDLE),
119 mQueue(VK_NULL_HANDLE),
120 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
121 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500122 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400123 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
124 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500125 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400126{
127}
128
129RendererVk::~RendererVk()
130{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500131 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500132 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500133 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
134 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500135 if (error.isError())
136 {
137 ERR() << "Error during VK shutdown: " << error;
138 }
139 }
140
Jamie Madill8c3988c2017-12-21 14:44:56 -0500141 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
142 {
143 descriptorSetLayout.destroy(mDevice);
144 }
145
146 mGraphicsPipelineLayout.destroy(mDevice);
147
Jamie Madill9f2a8612017-11-30 12:43:09 -0500148 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500149 mPipelineCache.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500150
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500151 if (mGlslangWrapper)
152 {
153 GlslangWrapper::ReleaseReference();
154 mGlslangWrapper = nullptr;
155 }
156
Jamie Madill5deea722017-02-16 10:44:46 -0500157 if (mCommandPool.valid())
158 {
159 mCommandPool.destroy(mDevice);
160 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500161
162 if (mDevice)
163 {
164 vkDestroyDevice(mDevice, nullptr);
165 mDevice = VK_NULL_HANDLE;
166 }
167
Jamie Madill0448ec82016-12-23 13:41:47 -0500168 if (mDebugReportCallback)
169 {
170 ASSERT(mInstance);
171 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
172 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
173 ASSERT(destroyDebugReportCallback);
174 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
175 }
176
Jamie Madill4d0bf552016-12-28 15:45:24 -0500177 if (mInstance)
178 {
179 vkDestroyInstance(mInstance, nullptr);
180 mInstance = VK_NULL_HANDLE;
181 }
182
183 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500184}
185
Frank Henigman29f148b2016-11-23 21:05:36 -0500186vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500187{
Jamie Madill222c5172017-07-19 16:15:42 -0400188 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500189
190 // If we're loading the validation layers, we could be running from any random directory.
191 // Change to the executable directory so we can find the layers, then change back to the
192 // previous directory to be safe we don't disrupt the application.
193 std::string previousCWD;
194
195 if (mEnableValidationLayers)
196 {
197 const auto &cwd = angle::GetCWD();
198 if (!cwd.valid())
199 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500200 ERR() << "Error getting CWD for Vulkan layers init.";
Jamie Madilla66779f2017-01-06 10:43:44 -0500201 mEnableValidationLayers = false;
202 }
203 else
204 {
205 previousCWD = cwd.value();
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400206 const char *exeDir = angle::GetExecutableDirectory();
207 if (!angle::SetCWD(exeDir))
208 {
209 ERR() << "Error setting CWD for Vulkan layers init.";
210 mEnableValidationLayers = false;
211 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500212 }
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400213 }
214
215 // Override environment variable to use the ANGLE layers.
216 if (mEnableValidationLayers)
217 {
218 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
219 {
220 ERR() << "Error setting environment for Vulkan layers init.";
221 mEnableValidationLayers = false;
222 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500223 }
224
Jamie Madill0448ec82016-12-23 13:41:47 -0500225 // Gather global layer properties.
226 uint32_t instanceLayerCount = 0;
227 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
228
229 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
230 if (instanceLayerCount > 0)
231 {
232 ANGLE_VK_TRY(
233 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
234 }
235
Jamie Madille09bd5d2016-11-29 16:20:35 -0500236 uint32_t instanceExtensionCount = 0;
237 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
238
239 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
240 if (instanceExtensionCount > 0)
241 {
242 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
243 instanceExtensionProps.data()));
244 }
245
Jamie Madill0448ec82016-12-23 13:41:47 -0500246 if (mEnableValidationLayers)
247 {
248 // Verify the standard validation layers are available.
249 if (!HasStandardValidationLayer(instanceLayerProps))
250 {
251 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400252 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
253 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500254 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500255 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500256 }
257 else
258 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500259 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500260 }
261 mEnableValidationLayers = false;
262 }
263 }
264
Jamie Madille09bd5d2016-11-29 16:20:35 -0500265 std::vector<const char *> enabledInstanceExtensions;
266 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500267 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500268
Jamie Madill0448ec82016-12-23 13:41:47 -0500269 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
270 if (mEnableValidationLayers)
271 {
272 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
273 }
274
Jamie Madille09bd5d2016-11-29 16:20:35 -0500275 // Verify the required extensions are in the extension names set. Fail if not.
276 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
277
Jamie Madill327ba852016-11-30 12:38:28 -0500278 VkApplicationInfo applicationInfo;
279 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
280 applicationInfo.pNext = nullptr;
281 applicationInfo.pApplicationName = "ANGLE";
282 applicationInfo.applicationVersion = 1;
283 applicationInfo.pEngineName = "ANGLE";
284 applicationInfo.engineVersion = 1;
285 applicationInfo.apiVersion = VK_API_VERSION_1_0;
286
287 VkInstanceCreateInfo instanceInfo;
288 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
289 instanceInfo.pNext = nullptr;
290 instanceInfo.flags = 0;
291 instanceInfo.pApplicationInfo = &applicationInfo;
292
Jamie Madille09bd5d2016-11-29 16:20:35 -0500293 // Enable requested layers and extensions.
294 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
295 instanceInfo.ppEnabledExtensionNames =
296 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500297 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
298 instanceInfo.ppEnabledLayerNames =
299 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500300
301 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
302
Jamie Madill0448ec82016-12-23 13:41:47 -0500303 if (mEnableValidationLayers)
304 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500305 // Change back to the previous working directory now that we've loaded the instance -
306 // the validation layers should be loaded at this point.
307 angle::SetCWD(previousCWD.c_str());
308
Jamie Madill0448ec82016-12-23 13:41:47 -0500309 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
310
311 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
312 debugReportInfo.pNext = nullptr;
313 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
314 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
315 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
316 debugReportInfo.pfnCallback = &DebugReportCallback;
317 debugReportInfo.pUserData = this;
318
319 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
320 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
321 ASSERT(createDebugReportCallback);
322 ANGLE_VK_TRY(
323 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
324 }
325
Jamie Madill4d0bf552016-12-28 15:45:24 -0500326 uint32_t physicalDeviceCount = 0;
327 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
328 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
329
330 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
331 physicalDeviceCount = 1;
332 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
333
334 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
335
336 // Ensure we can find a graphics queue family.
337 uint32_t queueCount = 0;
338 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
339
340 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
341
342 mQueueFamilyProperties.resize(queueCount);
343 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
344 mQueueFamilyProperties.data());
345
346 size_t graphicsQueueFamilyCount = false;
347 uint32_t firstGraphicsQueueFamily = 0;
348 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
349 {
350 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
351 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
352 {
353 ASSERT(queueInfo.queueCount > 0);
354 graphicsQueueFamilyCount++;
355 if (firstGraphicsQueueFamily == 0)
356 {
357 firstGraphicsQueueFamily = familyIndex;
358 }
359 break;
360 }
361 }
362
363 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
364
365 // If only one queue family, go ahead and initialize the device. If there is more than one
366 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
367 if (graphicsQueueFamilyCount == 1)
368 {
369 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
370 }
371
Jamie Madill035fd6b2017-10-03 15:43:22 -0400372 // Store the physical device memory properties so we can find the right memory pools.
373 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500374
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500375 mGlslangWrapper = GlslangWrapper::GetReference();
376
Jamie Madill6a89d222017-11-02 11:59:51 -0400377 // Initialize the format table.
378 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps);
379
Jamie Madill8c3988c2017-12-21 14:44:56 -0500380 // Initialize the pipeline layout for GL programs.
381 ANGLE_TRY(initGraphicsPipelineLayout());
382
Jamie Madill327ba852016-11-30 12:38:28 -0500383 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400384}
385
Jamie Madill4d0bf552016-12-28 15:45:24 -0500386vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
387{
388 uint32_t deviceLayerCount = 0;
389 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
390
391 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
392 if (deviceLayerCount > 0)
393 {
394 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
395 deviceLayerProps.data()));
396 }
397
398 uint32_t deviceExtensionCount = 0;
399 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
400 &deviceExtensionCount, nullptr));
401
402 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
403 if (deviceExtensionCount > 0)
404 {
405 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
406 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
407 }
408
409 if (mEnableValidationLayers)
410 {
411 if (!HasStandardValidationLayer(deviceLayerProps))
412 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500413 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500414 mEnableValidationLayers = false;
415 }
416 }
417
418 std::vector<const char *> enabledDeviceExtensions;
419 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
420
421 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
422
423 VkDeviceQueueCreateInfo queueCreateInfo;
424
425 float zeroPriority = 0.0f;
426
427 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
428 queueCreateInfo.pNext = nullptr;
429 queueCreateInfo.flags = 0;
430 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
431 queueCreateInfo.queueCount = 1;
432 queueCreateInfo.pQueuePriorities = &zeroPriority;
433
434 // Initialize the device
435 VkDeviceCreateInfo createInfo;
436
437 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
438 createInfo.pNext = nullptr;
439 createInfo.flags = 0;
440 createInfo.queueCreateInfoCount = 1;
441 createInfo.pQueueCreateInfos = &queueCreateInfo;
442 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
443 createInfo.ppEnabledLayerNames =
444 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
445 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
446 createInfo.ppEnabledExtensionNames =
447 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
448 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
449
450 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
451
452 mCurrentQueueFamilyIndex = queueFamilyIndex;
453
454 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
455
456 // Initialize the command pool now that we know the queue family index.
457 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500458 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
459 commandPoolInfo.pNext = nullptr;
460 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500461 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
462
Jamie Madill5deea722017-02-16 10:44:46 -0500463 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500464
Jamie Madill4d0bf552016-12-28 15:45:24 -0500465 return vk::NoError();
466}
467
468vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
469{
470 // We've already initialized a device, and can't re-create it unless it's never been used.
471 // TODO(jmadill): Handle the re-creation case if necessary.
472 if (mDevice != VK_NULL_HANDLE)
473 {
474 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
475
476 // Check if the current device supports present on this surface.
477 VkBool32 supportsPresent = VK_FALSE;
478 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
479 surface, &supportsPresent));
480
481 return (supportsPresent == VK_TRUE);
482 }
483
484 // Find a graphics and present queue.
485 Optional<uint32_t> newPresentQueue;
486 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
487 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
488 {
489 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
490 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
491 {
492 VkBool32 supportsPresent = VK_FALSE;
493 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
494 &supportsPresent));
495
496 if (supportsPresent == VK_TRUE)
497 {
498 newPresentQueue = queueIndex;
499 break;
500 }
501 }
502 }
503
504 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
505 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
506
507 return newPresentQueue.value();
508}
509
510std::string RendererVk::getVendorString() const
511{
512 switch (mPhysicalDeviceProperties.vendorID)
513 {
514 case VENDOR_ID_AMD:
515 return "Advanced Micro Devices";
516 case VENDOR_ID_NVIDIA:
517 return "NVIDIA";
518 case VENDOR_ID_INTEL:
519 return "Intel";
520 default:
521 {
522 // TODO(jmadill): More vendor IDs.
523 std::stringstream strstr;
524 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
525 return strstr.str();
526 }
527 }
528}
529
Jamie Madille09bd5d2016-11-29 16:20:35 -0500530std::string RendererVk::getRendererDescription() const
531{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500532 std::stringstream strstr;
533
534 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
535
536 strstr << "Vulkan ";
537 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
538 strstr << VK_VERSION_MINOR(apiVersion) << ".";
539 strstr << VK_VERSION_PATCH(apiVersion);
540
541 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
542
543 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500544}
545
Jamie Madillacccc6c2016-05-03 17:22:10 -0400546void RendererVk::ensureCapsInitialized() const
547{
548 if (!mCapsInitialized)
549 {
Luc Ferrone4741fd2018-01-25 13:25:27 -0500550 vk::GenerateCaps(mPhysicalDeviceProperties, &mNativeCaps, &mNativeTextureCaps,
551 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400552 mCapsInitialized = true;
553 }
554}
555
Jamie Madillacccc6c2016-05-03 17:22:10 -0400556const gl::Caps &RendererVk::getNativeCaps() const
557{
558 ensureCapsInitialized();
559 return mNativeCaps;
560}
561
562const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
563{
564 ensureCapsInitialized();
565 return mNativeTextureCaps;
566}
567
568const gl::Extensions &RendererVk::getNativeExtensions() const
569{
570 ensureCapsInitialized();
571 return mNativeExtensions;
572}
573
574const gl::Limitations &RendererVk::getNativeLimitations() const
575{
576 ensureCapsInitialized();
577 return mNativeLimitations;
578}
579
Jamie Madill49ac74b2017-12-21 14:42:33 -0500580const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500581{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500582 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500583}
584
Jamie Madill49ac74b2017-12-21 14:42:33 -0500585vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500586{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500587 if (!mOpenCommandGraph.empty())
588 {
589 vk::CommandBuffer commandBatch;
590 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400591
Jamie Madill49ac74b2017-12-21 14:42:33 -0500592 VkSubmitInfo submitInfo;
593 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
594 submitInfo.pNext = nullptr;
595 submitInfo.waitSemaphoreCount = 0;
596 submitInfo.pWaitSemaphores = nullptr;
597 submitInfo.pWaitDstStageMask = nullptr;
598 submitInfo.commandBufferCount = 1;
599 submitInfo.pCommandBuffers = commandBatch.ptr();
600 submitInfo.signalSemaphoreCount = 0;
601 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500602
Jamie Madill49ac74b2017-12-21 14:42:33 -0500603 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
604 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500605
Jamie Madill4c26fc22017-02-24 11:04:10 -0500606 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500607 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400608 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500609 return vk::NoError();
610}
611
Jamie Madill0c0dc342017-03-24 14:18:51 -0400612void RendererVk::freeAllInFlightResources()
613{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500614 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400615 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500616 batch.fence.destroy(mDevice);
617 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400618 }
619 mInFlightCommands.clear();
620
621 for (auto &garbage : mGarbage)
622 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400623 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400624 }
625 mGarbage.clear();
626}
627
Jamie Madill4c26fc22017-02-24 11:04:10 -0500628vk::Error RendererVk::checkInFlightCommands()
629{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500630 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500631
Jamie Madill49ac74b2017-12-21 14:42:33 -0500632 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500633 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500634 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400635 if (result == VK_NOT_READY)
636 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500637
Jamie Madill0c0dc342017-03-24 14:18:51 -0400638 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500639 ASSERT(batch.serial > mLastCompletedQueueSerial);
640 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400641
Jamie Madill49ac74b2017-12-21 14:42:33 -0500642 batch.fence.destroy(mDevice);
643 batch.commandPool.destroy(mDevice);
644 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500645 }
646
Jamie Madill49ac74b2017-12-21 14:42:33 -0500647 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400648
649 size_t freeIndex = 0;
650 for (; freeIndex < mGarbage.size(); ++freeIndex)
651 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500652 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400653 break;
654 }
655
656 // Remove the entries from the garbage list - they should be ready to go.
657 if (freeIndex > 0)
658 {
659 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500660 }
661
Jamie Madill4c26fc22017-02-24 11:04:10 -0500662 return vk::NoError();
663}
664
Jamie Madill49ac74b2017-12-21 14:42:33 -0500665vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500666{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500667 VkFenceCreateInfo fenceInfo;
668 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
669 fenceInfo.pNext = nullptr;
670 fenceInfo.flags = 0;
671
672 CommandBatch batch;
673 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
674
675 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500676
677 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500678 batch.commandPool = std::move(mCommandPool);
679 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500680
Jamie Madill49ac74b2017-12-21 14:42:33 -0500681 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400682
683 // Sanity check.
684 ASSERT(mInFlightCommands.size() < 1000u);
685
686 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400687 // TODO(jmadill): Overflow check.
688 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400689
690 ANGLE_TRY(checkInFlightCommands());
691
Jamie Madill49ac74b2017-12-21 14:42:33 -0500692 // Simply null out the command buffer here - it was allocated using the command pool.
693 commandBuffer.releaseHandle();
694
695 // Reallocate the command pool for next frame.
696 // TODO(jmadill): Consider reusing command pools.
697 VkCommandPoolCreateInfo poolInfo;
698 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
699 poolInfo.pNext = nullptr;
700 poolInfo.flags = 0;
701 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
702
703 mCommandPool.init(mDevice, poolInfo);
704
Jamie Madill4c26fc22017-02-24 11:04:10 -0500705 return vk::NoError();
706}
707
Jamie Madill5deea722017-02-16 10:44:46 -0500708vk::Error RendererVk::createStagingImage(TextureDimension dimension,
709 const vk::Format &format,
710 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400711 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500712 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500713{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400714 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
Jamie Madill1d7be502017-10-29 18:06:50 -0400715 format.vkTextureFormat, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500716 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500717}
718
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500719GlslangWrapper *RendererVk::getGlslangWrapper()
720{
721 return mGlslangWrapper;
722}
723
Jamie Madill4c26fc22017-02-24 11:04:10 -0500724Serial RendererVk::getCurrentQueueSerial() const
725{
726 return mCurrentQueueSerial;
727}
728
Jamie Madill97760352017-11-09 13:08:29 -0500729bool RendererVk::isResourceInUse(const ResourceVk &resource)
730{
731 return isSerialInUse(resource.getQueueSerial());
732}
733
734bool RendererVk::isSerialInUse(Serial serial)
735{
736 return serial > mLastCompletedQueueSerial;
737}
738
Jamie Madill9f2a8612017-11-30 12:43:09 -0500739vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
740 vk::RenderPass **renderPassOut)
741{
742 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
743 renderPassOut);
744}
745
Jamie Madillbef918c2017-12-13 13:11:30 -0500746vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
747 const vk::AttachmentOpsArray &ops,
748 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500749{
Jamie Madillbef918c2017-12-13 13:11:30 -0500750 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
751 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500752}
753
Jamie Madill49ac74b2017-12-21 14:42:33 -0500754vk::CommandBufferNode *RendererVk::allocateCommandNode()
755{
756 // TODO(jmadill): Use a pool allocator for the CPU node allocations.
757 vk::CommandBufferNode *newCommands = new vk::CommandBufferNode();
758 mOpenCommandGraph.emplace_back(newCommands);
759 return newCommands;
760}
761
762vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
763{
764 VkCommandBufferAllocateInfo primaryInfo;
765 primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
766 primaryInfo.pNext = nullptr;
767 primaryInfo.commandPool = mCommandPool.getHandle();
768 primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
769 primaryInfo.commandBufferCount = 1;
770
771 ANGLE_TRY(commandBatch->init(mDevice, primaryInfo));
772
773 if (mOpenCommandGraph.empty())
774 {
775 return vk::NoError();
776 }
777
778 std::vector<vk::CommandBufferNode *> nodeStack;
779
780 VkCommandBufferBeginInfo beginInfo;
781 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
782 beginInfo.pNext = nullptr;
783 beginInfo.flags = 0;
784 beginInfo.pInheritanceInfo = nullptr;
785
786 ANGLE_TRY(commandBatch->begin(beginInfo));
787
788 for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph)
789 {
790 // Only process commands that don't have child commands. The others will be pulled in
791 // automatically. Also skip commands that have already been visited.
792 if (topLevelNode->isDependency() ||
793 topLevelNode->visitedState() != vk::VisitedState::Unvisited)
794 continue;
795
796 nodeStack.push_back(topLevelNode);
797
798 while (!nodeStack.empty())
799 {
800 vk::CommandBufferNode *node = nodeStack.back();
801
802 switch (node->visitedState())
803 {
804 case vk::VisitedState::Unvisited:
805 node->visitDependencies(&nodeStack);
806 break;
807 case vk::VisitedState::Ready:
808 ANGLE_TRY(node->visitAndExecute(this, commandBatch));
809 nodeStack.pop_back();
810 break;
811 case vk::VisitedState::Visited:
812 nodeStack.pop_back();
813 break;
814 default:
815 UNREACHABLE();
816 break;
817 }
818 }
819 }
820
821 ANGLE_TRY(commandBatch->end());
Jamie Madill97f39b32018-01-05 13:14:29 -0500822 resetCommandGraph();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500823 return vk::NoError();
824}
825
826void RendererVk::resetCommandGraph()
827{
828 // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph.
829 for (vk::CommandBufferNode *node : mOpenCommandGraph)
830 {
831 delete node;
832 }
833 mOpenCommandGraph.clear();
834}
835
836vk::Error RendererVk::flush(const gl::Context *context,
837 const vk::Semaphore &waitSemaphore,
838 const vk::Semaphore &signalSemaphore)
839{
840 vk::CommandBuffer commandBatch;
841 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
842
843 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
844
845 VkSubmitInfo submitInfo;
846 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
847 submitInfo.pNext = nullptr;
848 submitInfo.waitSemaphoreCount = 1;
849 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
850 submitInfo.pWaitDstStageMask = &waitStageMask;
851 submitInfo.commandBufferCount = 1;
852 submitInfo.pCommandBuffers = commandBatch.ptr();
853 submitInfo.signalSemaphoreCount = 1;
854 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
855
856 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
857 return vk::NoError();
858}
859
Jamie Madill8c3988c2017-12-21 14:44:56 -0500860const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
861{
862 return mGraphicsPipelineLayout;
863}
864
865const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
866{
867 return mGraphicsDescriptorSetLayouts;
868}
869
870vk::Error RendererVk::initGraphicsPipelineLayout()
871{
872 ASSERT(!mGraphicsPipelineLayout.valid());
873
874 // Create two descriptor set layouts: one for default uniform info, and one for textures.
875 // Skip one or both if there are no uniforms.
876 VkDescriptorSetLayoutBinding uniformBindings[2];
877 uint32_t blockCount = 0;
878
879 {
880 auto &layoutBinding = uniformBindings[blockCount];
881
882 layoutBinding.binding = blockCount;
883 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
884 layoutBinding.descriptorCount = 1;
885 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
886 layoutBinding.pImmutableSamplers = nullptr;
887
888 blockCount++;
889 }
890
891 {
892 auto &layoutBinding = uniformBindings[blockCount];
893
894 layoutBinding.binding = blockCount;
895 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
896 layoutBinding.descriptorCount = 1;
897 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
898 layoutBinding.pImmutableSamplers = nullptr;
899
900 blockCount++;
901 }
902
903 {
904 VkDescriptorSetLayoutCreateInfo uniformInfo;
905 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
906 uniformInfo.pNext = nullptr;
907 uniformInfo.flags = 0;
908 uniformInfo.bindingCount = blockCount;
909 uniformInfo.pBindings = uniformBindings;
910
911 vk::DescriptorSetLayout uniformLayout;
912 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
913 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
914 }
915
Yuly Novikov37968132018-01-23 18:19:29 -0500916 // TODO(lucferron): expose this limitation to GL in Context Caps
917 std::vector<VkDescriptorSetLayoutBinding> textureBindings(
918 std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
919 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES));
Jamie Madill8c3988c2017-12-21 14:44:56 -0500920
921 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500922 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500923 {
924 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
925
926 layoutBinding.binding = textureIndex;
927 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
928 layoutBinding.descriptorCount = 1;
929 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
930 layoutBinding.pImmutableSamplers = nullptr;
931 }
932
933 {
934 VkDescriptorSetLayoutCreateInfo textureInfo;
935 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
936 textureInfo.pNext = nullptr;
937 textureInfo.flags = 0;
938 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
939 textureInfo.pBindings = textureBindings.data();
940
941 vk::DescriptorSetLayout textureLayout;
942 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
943 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
944 }
945
946 VkPipelineLayoutCreateInfo createInfo;
947 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
948 createInfo.pNext = nullptr;
949 createInfo.flags = 0;
950 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
951 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
952 createInfo.pushConstantRangeCount = 0;
953 createInfo.pPushConstantRanges = nullptr;
954
955 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
956
957 return vk::NoError();
958}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500959
960Serial RendererVk::issueProgramSerial()
961{
962 return mProgramSerialFactory.generate();
963}
964
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500965vk::Error RendererVk::getPipeline(const ProgramVk *programVk,
966 const vk::PipelineDesc &desc,
967 vk::PipelineAndSerial **pipelineOut)
968{
969 ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial);
970 ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial);
971
972 // Pull in a compatible RenderPass.
973 vk::RenderPass *compatibleRenderPass = nullptr;
974 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
975
976 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
977 programVk->getLinkedVertexModule(),
978 programVk->getLinkedFragmentModule(), desc, pipelineOut);
979}
980
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400981} // namespace rx