blob: ebd6d92eaa042f35bf208831be15f37d5f46fd3d [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"
Jamie Madill3c424b42018-01-19 12:35:09 -050027#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050028#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040029
30namespace rx
31{
32
Jamie Madille09bd5d2016-11-29 16:20:35 -050033namespace
34{
35
36VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
37 const std::vector<const char *> &enabledExtensionNames)
38{
39 // Compile the extensions names into a set.
40 std::set<std::string> extensionNames;
41 for (const auto &extensionProp : extensionProps)
42 {
43 extensionNames.insert(extensionProp.extensionName);
44 }
45
Jamie Madillacf2f3a2017-11-21 19:22:44 -050046 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050047 {
48 if (extensionNames.count(extensionName) == 0)
49 {
50 return VK_ERROR_EXTENSION_NOT_PRESENT;
51 }
52 }
53
54 return VK_SUCCESS;
55}
56
Yuly Novikov2e551f62018-01-24 21:45:34 -050057VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
58 VkDebugReportObjectTypeEXT objectType,
59 uint64_t object,
60 size_t location,
61 int32_t messageCode,
62 const char *layerPrefix,
63 const char *message,
64 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050065{
66 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
67 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050068 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050069#if !defined(NDEBUG)
70 // Abort the call in Debug builds.
71 return VK_TRUE;
72#endif
73 }
74 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
75 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050076 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050077 }
78 else
79 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050080 // Uncomment this if you want Vulkan spam.
81 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050082 }
83
84 return VK_FALSE;
85}
86
Jamie Madille09bd5d2016-11-29 16:20:35 -050087} // anonymous namespace
88
Jamie Madill49ac74b2017-12-21 14:42:33 -050089// CommandBatch implementation.
90RendererVk::CommandBatch::CommandBatch()
91{
92}
93
94RendererVk::CommandBatch::~CommandBatch()
95{
96}
97
98RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
99 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
100{
101}
102
103RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
104{
105 std::swap(commandPool, other.commandPool);
106 std::swap(fence, other.fence);
107 std::swap(serial, other.serial);
108 return *this;
109}
110
Jamie Madill9f2a8612017-11-30 12:43:09 -0500111// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500112RendererVk::RendererVk()
113 : mCapsInitialized(false),
114 mInstance(VK_NULL_HANDLE),
115 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500116 mDebugReportCallback(VK_NULL_HANDLE),
117 mPhysicalDevice(VK_NULL_HANDLE),
118 mQueue(VK_NULL_HANDLE),
119 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
120 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500121 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400122 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
123 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500124 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400125{
126}
127
128RendererVk::~RendererVk()
129{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500130 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500131 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500132 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
133 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500134 if (error.isError())
135 {
136 ERR() << "Error during VK shutdown: " << error;
137 }
138 }
139
Jamie Madill8c3988c2017-12-21 14:44:56 -0500140 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
141 {
142 descriptorSetLayout.destroy(mDevice);
143 }
144
145 mGraphicsPipelineLayout.destroy(mDevice);
146
Jamie Madill9f2a8612017-11-30 12:43:09 -0500147 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500148 mPipelineCache.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500149
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500150 if (mGlslangWrapper)
151 {
152 GlslangWrapper::ReleaseReference();
153 mGlslangWrapper = nullptr;
154 }
155
Jamie Madill5deea722017-02-16 10:44:46 -0500156 if (mCommandPool.valid())
157 {
158 mCommandPool.destroy(mDevice);
159 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500160
161 if (mDevice)
162 {
163 vkDestroyDevice(mDevice, nullptr);
164 mDevice = VK_NULL_HANDLE;
165 }
166
Jamie Madill0448ec82016-12-23 13:41:47 -0500167 if (mDebugReportCallback)
168 {
169 ASSERT(mInstance);
170 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
171 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
172 ASSERT(destroyDebugReportCallback);
173 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
174 }
175
Jamie Madill4d0bf552016-12-28 15:45:24 -0500176 if (mInstance)
177 {
178 vkDestroyInstance(mInstance, nullptr);
179 mInstance = VK_NULL_HANDLE;
180 }
181
182 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500183}
184
Frank Henigman29f148b2016-11-23 21:05:36 -0500185vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500186{
Jamie Madill222c5172017-07-19 16:15:42 -0400187 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500188
189 // If we're loading the validation layers, we could be running from any random directory.
190 // Change to the executable directory so we can find the layers, then change back to the
191 // previous directory to be safe we don't disrupt the application.
192 std::string previousCWD;
193
194 if (mEnableValidationLayers)
195 {
196 const auto &cwd = angle::GetCWD();
197 if (!cwd.valid())
198 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500199 ERR() << "Error getting CWD for Vulkan layers init.";
Jamie Madilla66779f2017-01-06 10:43:44 -0500200 mEnableValidationLayers = false;
201 }
202 else
203 {
204 previousCWD = cwd.value();
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400205 const char *exeDir = angle::GetExecutableDirectory();
206 if (!angle::SetCWD(exeDir))
207 {
208 ERR() << "Error setting CWD for Vulkan layers init.";
209 mEnableValidationLayers = false;
210 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500211 }
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400212 }
213
214 // Override environment variable to use the ANGLE layers.
215 if (mEnableValidationLayers)
216 {
217 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
218 {
219 ERR() << "Error setting environment for Vulkan layers init.";
220 mEnableValidationLayers = false;
221 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500222 }
223
Jamie Madill0448ec82016-12-23 13:41:47 -0500224 // Gather global layer properties.
225 uint32_t instanceLayerCount = 0;
226 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
227
228 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
229 if (instanceLayerCount > 0)
230 {
231 ANGLE_VK_TRY(
232 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
233 }
234
Jamie Madille09bd5d2016-11-29 16:20:35 -0500235 uint32_t instanceExtensionCount = 0;
236 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
237
238 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
239 if (instanceExtensionCount > 0)
240 {
241 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
242 instanceExtensionProps.data()));
243 }
244
Jamie Madill0448ec82016-12-23 13:41:47 -0500245 if (mEnableValidationLayers)
246 {
247 // Verify the standard validation layers are available.
248 if (!HasStandardValidationLayer(instanceLayerProps))
249 {
250 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400251 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
252 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500253 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500254 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500255 }
256 else
257 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500258 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500259 }
260 mEnableValidationLayers = false;
261 }
262 }
263
Jamie Madille09bd5d2016-11-29 16:20:35 -0500264 std::vector<const char *> enabledInstanceExtensions;
265 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500266 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500267
Jamie Madill0448ec82016-12-23 13:41:47 -0500268 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
269 if (mEnableValidationLayers)
270 {
271 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
272 }
273
Jamie Madille09bd5d2016-11-29 16:20:35 -0500274 // Verify the required extensions are in the extension names set. Fail if not.
275 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
276
Jamie Madill327ba852016-11-30 12:38:28 -0500277 VkApplicationInfo applicationInfo;
278 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
279 applicationInfo.pNext = nullptr;
280 applicationInfo.pApplicationName = "ANGLE";
281 applicationInfo.applicationVersion = 1;
282 applicationInfo.pEngineName = "ANGLE";
283 applicationInfo.engineVersion = 1;
284 applicationInfo.apiVersion = VK_API_VERSION_1_0;
285
286 VkInstanceCreateInfo instanceInfo;
287 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
288 instanceInfo.pNext = nullptr;
289 instanceInfo.flags = 0;
290 instanceInfo.pApplicationInfo = &applicationInfo;
291
Jamie Madille09bd5d2016-11-29 16:20:35 -0500292 // Enable requested layers and extensions.
293 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
294 instanceInfo.ppEnabledExtensionNames =
295 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500296 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
297 instanceInfo.ppEnabledLayerNames =
298 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500299
300 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
301
Jamie Madill0448ec82016-12-23 13:41:47 -0500302 if (mEnableValidationLayers)
303 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500304 // Change back to the previous working directory now that we've loaded the instance -
305 // the validation layers should be loaded at this point.
306 angle::SetCWD(previousCWD.c_str());
307
Jamie Madill0448ec82016-12-23 13:41:47 -0500308 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
309
310 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
311 debugReportInfo.pNext = nullptr;
312 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
313 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
314 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
315 debugReportInfo.pfnCallback = &DebugReportCallback;
316 debugReportInfo.pUserData = this;
317
318 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
319 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
320 ASSERT(createDebugReportCallback);
321 ANGLE_VK_TRY(
322 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
323 }
324
Jamie Madill4d0bf552016-12-28 15:45:24 -0500325 uint32_t physicalDeviceCount = 0;
326 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
327 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
328
329 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
330 physicalDeviceCount = 1;
331 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
332
333 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
334
335 // Ensure we can find a graphics queue family.
336 uint32_t queueCount = 0;
337 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
338
339 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
340
341 mQueueFamilyProperties.resize(queueCount);
342 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
343 mQueueFamilyProperties.data());
344
345 size_t graphicsQueueFamilyCount = false;
346 uint32_t firstGraphicsQueueFamily = 0;
347 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
348 {
349 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
350 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
351 {
352 ASSERT(queueInfo.queueCount > 0);
353 graphicsQueueFamilyCount++;
354 if (firstGraphicsQueueFamily == 0)
355 {
356 firstGraphicsQueueFamily = familyIndex;
357 }
358 break;
359 }
360 }
361
362 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
363
364 // If only one queue family, go ahead and initialize the device. If there is more than one
365 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
366 if (graphicsQueueFamilyCount == 1)
367 {
368 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
369 }
370
Jamie Madill035fd6b2017-10-03 15:43:22 -0400371 // Store the physical device memory properties so we can find the right memory pools.
372 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500373
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500374 mGlslangWrapper = GlslangWrapper::GetReference();
375
Jamie Madill6a89d222017-11-02 11:59:51 -0400376 // Initialize the format table.
377 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps);
378
Jamie Madill8c3988c2017-12-21 14:44:56 -0500379 // Initialize the pipeline layout for GL programs.
380 ANGLE_TRY(initGraphicsPipelineLayout());
381
Jamie Madill327ba852016-11-30 12:38:28 -0500382 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400383}
384
Jamie Madill4d0bf552016-12-28 15:45:24 -0500385vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
386{
387 uint32_t deviceLayerCount = 0;
388 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
389
390 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
391 if (deviceLayerCount > 0)
392 {
393 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
394 deviceLayerProps.data()));
395 }
396
397 uint32_t deviceExtensionCount = 0;
398 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
399 &deviceExtensionCount, nullptr));
400
401 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
402 if (deviceExtensionCount > 0)
403 {
404 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
405 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
406 }
407
408 if (mEnableValidationLayers)
409 {
410 if (!HasStandardValidationLayer(deviceLayerProps))
411 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500412 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500413 mEnableValidationLayers = false;
414 }
415 }
416
417 std::vector<const char *> enabledDeviceExtensions;
418 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
419
420 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
421
422 VkDeviceQueueCreateInfo queueCreateInfo;
423
424 float zeroPriority = 0.0f;
425
426 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
427 queueCreateInfo.pNext = nullptr;
428 queueCreateInfo.flags = 0;
429 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
430 queueCreateInfo.queueCount = 1;
431 queueCreateInfo.pQueuePriorities = &zeroPriority;
432
433 // Initialize the device
434 VkDeviceCreateInfo createInfo;
435
436 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
437 createInfo.pNext = nullptr;
438 createInfo.flags = 0;
439 createInfo.queueCreateInfoCount = 1;
440 createInfo.pQueueCreateInfos = &queueCreateInfo;
441 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
442 createInfo.ppEnabledLayerNames =
443 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
444 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
445 createInfo.ppEnabledExtensionNames =
446 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
447 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
448
449 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
450
451 mCurrentQueueFamilyIndex = queueFamilyIndex;
452
453 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
454
455 // Initialize the command pool now that we know the queue family index.
456 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500457 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
458 commandPoolInfo.pNext = nullptr;
459 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500460 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
461
Jamie Madill5deea722017-02-16 10:44:46 -0500462 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500463
Jamie Madill4d0bf552016-12-28 15:45:24 -0500464 return vk::NoError();
465}
466
467vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
468{
469 // We've already initialized a device, and can't re-create it unless it's never been used.
470 // TODO(jmadill): Handle the re-creation case if necessary.
471 if (mDevice != VK_NULL_HANDLE)
472 {
473 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
474
475 // Check if the current device supports present on this surface.
476 VkBool32 supportsPresent = VK_FALSE;
477 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
478 surface, &supportsPresent));
479
480 return (supportsPresent == VK_TRUE);
481 }
482
483 // Find a graphics and present queue.
484 Optional<uint32_t> newPresentQueue;
485 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
486 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
487 {
488 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
489 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
490 {
491 VkBool32 supportsPresent = VK_FALSE;
492 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
493 &supportsPresent));
494
495 if (supportsPresent == VK_TRUE)
496 {
497 newPresentQueue = queueIndex;
498 break;
499 }
500 }
501 }
502
503 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
504 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
505
506 return newPresentQueue.value();
507}
508
509std::string RendererVk::getVendorString() const
510{
511 switch (mPhysicalDeviceProperties.vendorID)
512 {
513 case VENDOR_ID_AMD:
514 return "Advanced Micro Devices";
515 case VENDOR_ID_NVIDIA:
516 return "NVIDIA";
517 case VENDOR_ID_INTEL:
518 return "Intel";
519 default:
520 {
521 // TODO(jmadill): More vendor IDs.
522 std::stringstream strstr;
523 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
524 return strstr.str();
525 }
526 }
527}
528
Jamie Madille09bd5d2016-11-29 16:20:35 -0500529std::string RendererVk::getRendererDescription() const
530{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500531 std::stringstream strstr;
532
533 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
534
535 strstr << "Vulkan ";
536 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
537 strstr << VK_VERSION_MINOR(apiVersion) << ".";
538 strstr << VK_VERSION_PATCH(apiVersion);
539
540 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
541
542 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500543}
544
Jamie Madillacccc6c2016-05-03 17:22:10 -0400545void RendererVk::ensureCapsInitialized() const
546{
547 if (!mCapsInitialized)
548 {
549 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
550 mCapsInitialized = true;
551 }
552}
553
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500554void RendererVk::generateCaps(gl::Caps *outCaps,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400555 gl::TextureCapsMap * /*outTextureCaps*/,
Jamie Madillb8353b02017-01-25 12:57:21 -0800556 gl::Extensions *outExtensions,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400557 gl::Limitations * /* outLimitations */) const
558{
Jamie Madill327ba852016-11-30 12:38:28 -0500559 // TODO(jmadill): Caps.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500560 outCaps->maxDrawBuffers = 1;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800561 outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS;
562 outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS;
Jamie Madill035fd6b2017-10-03 15:43:22 -0400563 outCaps->maxVaryingVectors = 16;
564 outCaps->maxTextureImageUnits = 1;
565 outCaps->maxCombinedTextureImageUnits = 1;
566 outCaps->max2DTextureSize = 1024;
Jamie Madilld03a8492017-10-03 15:46:06 -0400567 outCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
Jamie Madill6276b922017-09-25 02:35:57 -0400568 outCaps->maxFragmentUniformVectors = 8;
569 outCaps->maxVertexUniformVectors = 8;
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400570 outCaps->maxColorAttachments = 1;
Jamie Madillb8353b02017-01-25 12:57:21 -0800571
572 // Enable this for simple buffer readback testing, but some functionality is missing.
573 // TODO(jmadill): Support full mapBufferRange extension.
574 outExtensions->mapBuffer = true;
575 outExtensions->mapBufferRange = true;
Jamie Madillacccc6c2016-05-03 17:22:10 -0400576}
577
578const gl::Caps &RendererVk::getNativeCaps() const
579{
580 ensureCapsInitialized();
581 return mNativeCaps;
582}
583
584const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
585{
586 ensureCapsInitialized();
587 return mNativeTextureCaps;
588}
589
590const gl::Extensions &RendererVk::getNativeExtensions() const
591{
592 ensureCapsInitialized();
593 return mNativeExtensions;
594}
595
596const gl::Limitations &RendererVk::getNativeLimitations() const
597{
598 ensureCapsInitialized();
599 return mNativeLimitations;
600}
601
Jamie Madill49ac74b2017-12-21 14:42:33 -0500602const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500604 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500605}
606
Jamie Madill49ac74b2017-12-21 14:42:33 -0500607vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500608{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500609 if (!mOpenCommandGraph.empty())
610 {
611 vk::CommandBuffer commandBatch;
612 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400613
Jamie Madill49ac74b2017-12-21 14:42:33 -0500614 VkSubmitInfo submitInfo;
615 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
616 submitInfo.pNext = nullptr;
617 submitInfo.waitSemaphoreCount = 0;
618 submitInfo.pWaitSemaphores = nullptr;
619 submitInfo.pWaitDstStageMask = nullptr;
620 submitInfo.commandBufferCount = 1;
621 submitInfo.pCommandBuffers = commandBatch.ptr();
622 submitInfo.signalSemaphoreCount = 0;
623 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500624
Jamie Madill49ac74b2017-12-21 14:42:33 -0500625 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
626 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500627
Jamie Madill4c26fc22017-02-24 11:04:10 -0500628 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500629 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400630 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500631 return vk::NoError();
632}
633
Jamie Madill0c0dc342017-03-24 14:18:51 -0400634void RendererVk::freeAllInFlightResources()
635{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500636 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400637 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500638 batch.fence.destroy(mDevice);
639 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400640 }
641 mInFlightCommands.clear();
642
643 for (auto &garbage : mGarbage)
644 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400645 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400646 }
647 mGarbage.clear();
648}
649
Jamie Madill4c26fc22017-02-24 11:04:10 -0500650vk::Error RendererVk::checkInFlightCommands()
651{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500652 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500653
Jamie Madill49ac74b2017-12-21 14:42:33 -0500654 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500655 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500656 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400657 if (result == VK_NOT_READY)
658 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500659
Jamie Madill0c0dc342017-03-24 14:18:51 -0400660 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500661 ASSERT(batch.serial > mLastCompletedQueueSerial);
662 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400663
Jamie Madill49ac74b2017-12-21 14:42:33 -0500664 batch.fence.destroy(mDevice);
665 batch.commandPool.destroy(mDevice);
666 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500667 }
668
Jamie Madill49ac74b2017-12-21 14:42:33 -0500669 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400670
671 size_t freeIndex = 0;
672 for (; freeIndex < mGarbage.size(); ++freeIndex)
673 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500674 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400675 break;
676 }
677
678 // Remove the entries from the garbage list - they should be ready to go.
679 if (freeIndex > 0)
680 {
681 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500682 }
683
Jamie Madill4c26fc22017-02-24 11:04:10 -0500684 return vk::NoError();
685}
686
Jamie Madill49ac74b2017-12-21 14:42:33 -0500687vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500688{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500689 VkFenceCreateInfo fenceInfo;
690 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
691 fenceInfo.pNext = nullptr;
692 fenceInfo.flags = 0;
693
694 CommandBatch batch;
695 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
696
697 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500698
699 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500700 batch.commandPool = std::move(mCommandPool);
701 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500702
Jamie Madill49ac74b2017-12-21 14:42:33 -0500703 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400704
705 // Sanity check.
706 ASSERT(mInFlightCommands.size() < 1000u);
707
708 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400709 // TODO(jmadill): Overflow check.
710 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400711
712 ANGLE_TRY(checkInFlightCommands());
713
Jamie Madill49ac74b2017-12-21 14:42:33 -0500714 // Simply null out the command buffer here - it was allocated using the command pool.
715 commandBuffer.releaseHandle();
716
717 // Reallocate the command pool for next frame.
718 // TODO(jmadill): Consider reusing command pools.
719 VkCommandPoolCreateInfo poolInfo;
720 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
721 poolInfo.pNext = nullptr;
722 poolInfo.flags = 0;
723 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
724
725 mCommandPool.init(mDevice, poolInfo);
726
Jamie Madill4c26fc22017-02-24 11:04:10 -0500727 return vk::NoError();
728}
729
Jamie Madill5deea722017-02-16 10:44:46 -0500730vk::Error RendererVk::createStagingImage(TextureDimension dimension,
731 const vk::Format &format,
732 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400733 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500734 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500735{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400736 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
Jamie Madill1d7be502017-10-29 18:06:50 -0400737 format.vkTextureFormat, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500738 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500739}
740
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500741GlslangWrapper *RendererVk::getGlslangWrapper()
742{
743 return mGlslangWrapper;
744}
745
Jamie Madill4c26fc22017-02-24 11:04:10 -0500746Serial RendererVk::getCurrentQueueSerial() const
747{
748 return mCurrentQueueSerial;
749}
750
Jamie Madill97760352017-11-09 13:08:29 -0500751bool RendererVk::isResourceInUse(const ResourceVk &resource)
752{
753 return isSerialInUse(resource.getQueueSerial());
754}
755
756bool RendererVk::isSerialInUse(Serial serial)
757{
758 return serial > mLastCompletedQueueSerial;
759}
760
Jamie Madill9f2a8612017-11-30 12:43:09 -0500761vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
762 vk::RenderPass **renderPassOut)
763{
764 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
765 renderPassOut);
766}
767
Jamie Madillbef918c2017-12-13 13:11:30 -0500768vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
769 const vk::AttachmentOpsArray &ops,
770 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500771{
Jamie Madillbef918c2017-12-13 13:11:30 -0500772 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
773 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500774}
775
Jamie Madill49ac74b2017-12-21 14:42:33 -0500776vk::CommandBufferNode *RendererVk::allocateCommandNode()
777{
778 // TODO(jmadill): Use a pool allocator for the CPU node allocations.
779 vk::CommandBufferNode *newCommands = new vk::CommandBufferNode();
780 mOpenCommandGraph.emplace_back(newCommands);
781 return newCommands;
782}
783
784vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
785{
786 VkCommandBufferAllocateInfo primaryInfo;
787 primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
788 primaryInfo.pNext = nullptr;
789 primaryInfo.commandPool = mCommandPool.getHandle();
790 primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
791 primaryInfo.commandBufferCount = 1;
792
793 ANGLE_TRY(commandBatch->init(mDevice, primaryInfo));
794
795 if (mOpenCommandGraph.empty())
796 {
797 return vk::NoError();
798 }
799
800 std::vector<vk::CommandBufferNode *> nodeStack;
801
802 VkCommandBufferBeginInfo beginInfo;
803 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
804 beginInfo.pNext = nullptr;
805 beginInfo.flags = 0;
806 beginInfo.pInheritanceInfo = nullptr;
807
808 ANGLE_TRY(commandBatch->begin(beginInfo));
809
810 for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph)
811 {
812 // Only process commands that don't have child commands. The others will be pulled in
813 // automatically. Also skip commands that have already been visited.
814 if (topLevelNode->isDependency() ||
815 topLevelNode->visitedState() != vk::VisitedState::Unvisited)
816 continue;
817
818 nodeStack.push_back(topLevelNode);
819
820 while (!nodeStack.empty())
821 {
822 vk::CommandBufferNode *node = nodeStack.back();
823
824 switch (node->visitedState())
825 {
826 case vk::VisitedState::Unvisited:
827 node->visitDependencies(&nodeStack);
828 break;
829 case vk::VisitedState::Ready:
830 ANGLE_TRY(node->visitAndExecute(this, commandBatch));
831 nodeStack.pop_back();
832 break;
833 case vk::VisitedState::Visited:
834 nodeStack.pop_back();
835 break;
836 default:
837 UNREACHABLE();
838 break;
839 }
840 }
841 }
842
843 ANGLE_TRY(commandBatch->end());
Jamie Madill97f39b32018-01-05 13:14:29 -0500844 resetCommandGraph();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500845 return vk::NoError();
846}
847
848void RendererVk::resetCommandGraph()
849{
850 // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph.
851 for (vk::CommandBufferNode *node : mOpenCommandGraph)
852 {
853 delete node;
854 }
855 mOpenCommandGraph.clear();
856}
857
858vk::Error RendererVk::flush(const gl::Context *context,
859 const vk::Semaphore &waitSemaphore,
860 const vk::Semaphore &signalSemaphore)
861{
862 vk::CommandBuffer commandBatch;
863 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
864
865 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
866
867 VkSubmitInfo submitInfo;
868 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
869 submitInfo.pNext = nullptr;
870 submitInfo.waitSemaphoreCount = 1;
871 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
872 submitInfo.pWaitDstStageMask = &waitStageMask;
873 submitInfo.commandBufferCount = 1;
874 submitInfo.pCommandBuffers = commandBatch.ptr();
875 submitInfo.signalSemaphoreCount = 1;
876 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
877
878 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
879 return vk::NoError();
880}
881
Jamie Madill8c3988c2017-12-21 14:44:56 -0500882const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
883{
884 return mGraphicsPipelineLayout;
885}
886
887const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
888{
889 return mGraphicsDescriptorSetLayouts;
890}
891
892vk::Error RendererVk::initGraphicsPipelineLayout()
893{
894 ASSERT(!mGraphicsPipelineLayout.valid());
895
896 // Create two descriptor set layouts: one for default uniform info, and one for textures.
897 // Skip one or both if there are no uniforms.
898 VkDescriptorSetLayoutBinding uniformBindings[2];
899 uint32_t blockCount = 0;
900
901 {
902 auto &layoutBinding = uniformBindings[blockCount];
903
904 layoutBinding.binding = blockCount;
905 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
906 layoutBinding.descriptorCount = 1;
907 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
908 layoutBinding.pImmutableSamplers = nullptr;
909
910 blockCount++;
911 }
912
913 {
914 auto &layoutBinding = uniformBindings[blockCount];
915
916 layoutBinding.binding = blockCount;
917 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
918 layoutBinding.descriptorCount = 1;
919 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
920 layoutBinding.pImmutableSamplers = nullptr;
921
922 blockCount++;
923 }
924
925 {
926 VkDescriptorSetLayoutCreateInfo uniformInfo;
927 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
928 uniformInfo.pNext = nullptr;
929 uniformInfo.flags = 0;
930 uniformInfo.bindingCount = blockCount;
931 uniformInfo.pBindings = uniformBindings;
932
933 vk::DescriptorSetLayout uniformLayout;
934 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
935 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
936 }
937
Yuly Novikov37968132018-01-23 18:19:29 -0500938 // TODO(lucferron): expose this limitation to GL in Context Caps
939 std::vector<VkDescriptorSetLayoutBinding> textureBindings(
940 std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
941 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES));
Jamie Madill8c3988c2017-12-21 14:44:56 -0500942
943 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500944 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500945 {
946 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
947
948 layoutBinding.binding = textureIndex;
949 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
950 layoutBinding.descriptorCount = 1;
951 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
952 layoutBinding.pImmutableSamplers = nullptr;
953 }
954
955 {
956 VkDescriptorSetLayoutCreateInfo textureInfo;
957 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
958 textureInfo.pNext = nullptr;
959 textureInfo.flags = 0;
960 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
961 textureInfo.pBindings = textureBindings.data();
962
963 vk::DescriptorSetLayout textureLayout;
964 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
965 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
966 }
967
968 VkPipelineLayoutCreateInfo createInfo;
969 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
970 createInfo.pNext = nullptr;
971 createInfo.flags = 0;
972 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
973 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
974 createInfo.pushConstantRangeCount = 0;
975 createInfo.pPushConstantRanges = nullptr;
976
977 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
978
979 return vk::NoError();
980}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500981
982Serial RendererVk::issueProgramSerial()
983{
984 return mProgramSerialFactory.generate();
985}
986
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500987vk::Error RendererVk::getPipeline(const ProgramVk *programVk,
988 const vk::PipelineDesc &desc,
989 vk::PipelineAndSerial **pipelineOut)
990{
991 ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial);
992 ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial);
993
994 // Pull in a compatible RenderPass.
995 vk::RenderPass *compatibleRenderPass = nullptr;
996 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
997
998 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
999 programVk->getLinkedVertexModule(),
1000 programVk->getLinkedFragmentModule(), desc, pipelineOut);
1001}
1002
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001003} // namespace rx