blob: de844bcd99e97a69cc9f6a47fe2b1be4c8d06afb [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 Madille09bd5d2016-11-29 16:20:35 -050024#include "libANGLE/renderer/vulkan/TextureVk.h"
25#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050026#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050027#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040028
29namespace rx
30{
31
Jamie Madille09bd5d2016-11-29 16:20:35 -050032namespace
33{
34
35VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
36 const std::vector<const char *> &enabledExtensionNames)
37{
38 // Compile the extensions names into a set.
39 std::set<std::string> extensionNames;
40 for (const auto &extensionProp : extensionProps)
41 {
42 extensionNames.insert(extensionProp.extensionName);
43 }
44
Jamie Madillacf2f3a2017-11-21 19:22:44 -050045 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050046 {
47 if (extensionNames.count(extensionName) == 0)
48 {
49 return VK_ERROR_EXTENSION_NOT_PRESENT;
50 }
51 }
52
53 return VK_SUCCESS;
54}
55
Yuly Novikov2e551f62018-01-24 21:45:34 -050056VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
57 VkDebugReportObjectTypeEXT objectType,
58 uint64_t object,
59 size_t location,
60 int32_t messageCode,
61 const char *layerPrefix,
62 const char *message,
63 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050064{
65 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
66 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050067 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050068#if !defined(NDEBUG)
69 // Abort the call in Debug builds.
70 return VK_TRUE;
71#endif
72 }
73 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
74 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050075 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050076 }
77 else
78 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050079 // Uncomment this if you want Vulkan spam.
80 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050081 }
82
83 return VK_FALSE;
84}
85
Jamie Madille09bd5d2016-11-29 16:20:35 -050086} // anonymous namespace
87
Jamie Madill49ac74b2017-12-21 14:42:33 -050088// CommandBatch implementation.
89RendererVk::CommandBatch::CommandBatch()
90{
91}
92
93RendererVk::CommandBatch::~CommandBatch()
94{
95}
96
97RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
98 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
99{
100}
101
102RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
103{
104 std::swap(commandPool, other.commandPool);
105 std::swap(fence, other.fence);
106 std::swap(serial, other.serial);
107 return *this;
108}
109
Jamie Madill9f2a8612017-11-30 12:43:09 -0500110// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500111RendererVk::RendererVk()
112 : mCapsInitialized(false),
113 mInstance(VK_NULL_HANDLE),
114 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500115 mDebugReportCallback(VK_NULL_HANDLE),
116 mPhysicalDevice(VK_NULL_HANDLE),
117 mQueue(VK_NULL_HANDLE),
118 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
119 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500120 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400121 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
122 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500123 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400124{
125}
126
127RendererVk::~RendererVk()
128{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500129 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500130 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500131 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
132 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500133 if (error.isError())
134 {
135 ERR() << "Error during VK shutdown: " << error;
136 }
137 }
138
Jamie Madill8c3988c2017-12-21 14:44:56 -0500139 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
140 {
141 descriptorSetLayout.destroy(mDevice);
142 }
143
144 mGraphicsPipelineLayout.destroy(mDevice);
145
Jamie Madill9f2a8612017-11-30 12:43:09 -0500146 mRenderPassCache.destroy(mDevice);
147
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500148 if (mGlslangWrapper)
149 {
150 GlslangWrapper::ReleaseReference();
151 mGlslangWrapper = nullptr;
152 }
153
Jamie Madill5deea722017-02-16 10:44:46 -0500154 if (mCommandPool.valid())
155 {
156 mCommandPool.destroy(mDevice);
157 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500158
159 if (mDevice)
160 {
161 vkDestroyDevice(mDevice, nullptr);
162 mDevice = VK_NULL_HANDLE;
163 }
164
Jamie Madill0448ec82016-12-23 13:41:47 -0500165 if (mDebugReportCallback)
166 {
167 ASSERT(mInstance);
168 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
169 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
170 ASSERT(destroyDebugReportCallback);
171 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
172 }
173
Jamie Madill4d0bf552016-12-28 15:45:24 -0500174 if (mInstance)
175 {
176 vkDestroyInstance(mInstance, nullptr);
177 mInstance = VK_NULL_HANDLE;
178 }
179
180 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500181}
182
Frank Henigman29f148b2016-11-23 21:05:36 -0500183vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500184{
Jamie Madill222c5172017-07-19 16:15:42 -0400185 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500186
187 // If we're loading the validation layers, we could be running from any random directory.
188 // Change to the executable directory so we can find the layers, then change back to the
189 // previous directory to be safe we don't disrupt the application.
190 std::string previousCWD;
191
192 if (mEnableValidationLayers)
193 {
194 const auto &cwd = angle::GetCWD();
195 if (!cwd.valid())
196 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500197 ERR() << "Error getting CWD for Vulkan layers init.";
Jamie Madilla66779f2017-01-06 10:43:44 -0500198 mEnableValidationLayers = false;
199 }
200 else
201 {
202 previousCWD = cwd.value();
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400203 const char *exeDir = angle::GetExecutableDirectory();
204 if (!angle::SetCWD(exeDir))
205 {
206 ERR() << "Error setting CWD for Vulkan layers init.";
207 mEnableValidationLayers = false;
208 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500209 }
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400210 }
211
212 // Override environment variable to use the ANGLE layers.
213 if (mEnableValidationLayers)
214 {
215 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
216 {
217 ERR() << "Error setting environment for Vulkan layers init.";
218 mEnableValidationLayers = false;
219 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500220 }
221
Jamie Madill0448ec82016-12-23 13:41:47 -0500222 // Gather global layer properties.
223 uint32_t instanceLayerCount = 0;
224 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
225
226 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
227 if (instanceLayerCount > 0)
228 {
229 ANGLE_VK_TRY(
230 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
231 }
232
Jamie Madille09bd5d2016-11-29 16:20:35 -0500233 uint32_t instanceExtensionCount = 0;
234 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
235
236 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
237 if (instanceExtensionCount > 0)
238 {
239 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
240 instanceExtensionProps.data()));
241 }
242
Jamie Madill0448ec82016-12-23 13:41:47 -0500243 if (mEnableValidationLayers)
244 {
245 // Verify the standard validation layers are available.
246 if (!HasStandardValidationLayer(instanceLayerProps))
247 {
248 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400249 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
250 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500251 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500252 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500253 }
254 else
255 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500256 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500257 }
258 mEnableValidationLayers = false;
259 }
260 }
261
Jamie Madille09bd5d2016-11-29 16:20:35 -0500262 std::vector<const char *> enabledInstanceExtensions;
263 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500264 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500265
Jamie Madill0448ec82016-12-23 13:41:47 -0500266 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
267 if (mEnableValidationLayers)
268 {
269 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
270 }
271
Jamie Madille09bd5d2016-11-29 16:20:35 -0500272 // Verify the required extensions are in the extension names set. Fail if not.
273 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
274
Jamie Madill327ba852016-11-30 12:38:28 -0500275 VkApplicationInfo applicationInfo;
276 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
277 applicationInfo.pNext = nullptr;
278 applicationInfo.pApplicationName = "ANGLE";
279 applicationInfo.applicationVersion = 1;
280 applicationInfo.pEngineName = "ANGLE";
281 applicationInfo.engineVersion = 1;
282 applicationInfo.apiVersion = VK_API_VERSION_1_0;
283
284 VkInstanceCreateInfo instanceInfo;
285 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
286 instanceInfo.pNext = nullptr;
287 instanceInfo.flags = 0;
288 instanceInfo.pApplicationInfo = &applicationInfo;
289
Jamie Madille09bd5d2016-11-29 16:20:35 -0500290 // Enable requested layers and extensions.
291 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
292 instanceInfo.ppEnabledExtensionNames =
293 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500294 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
295 instanceInfo.ppEnabledLayerNames =
296 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500297
298 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
299
Jamie Madill0448ec82016-12-23 13:41:47 -0500300 if (mEnableValidationLayers)
301 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500302 // Change back to the previous working directory now that we've loaded the instance -
303 // the validation layers should be loaded at this point.
304 angle::SetCWD(previousCWD.c_str());
305
Jamie Madill0448ec82016-12-23 13:41:47 -0500306 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
307
308 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
309 debugReportInfo.pNext = nullptr;
310 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
311 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
312 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
313 debugReportInfo.pfnCallback = &DebugReportCallback;
314 debugReportInfo.pUserData = this;
315
316 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
317 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
318 ASSERT(createDebugReportCallback);
319 ANGLE_VK_TRY(
320 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
321 }
322
Jamie Madill4d0bf552016-12-28 15:45:24 -0500323 uint32_t physicalDeviceCount = 0;
324 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
325 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
326
327 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
328 physicalDeviceCount = 1;
329 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
330
331 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
332
333 // Ensure we can find a graphics queue family.
334 uint32_t queueCount = 0;
335 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
336
337 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
338
339 mQueueFamilyProperties.resize(queueCount);
340 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
341 mQueueFamilyProperties.data());
342
343 size_t graphicsQueueFamilyCount = false;
344 uint32_t firstGraphicsQueueFamily = 0;
345 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
346 {
347 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
348 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
349 {
350 ASSERT(queueInfo.queueCount > 0);
351 graphicsQueueFamilyCount++;
352 if (firstGraphicsQueueFamily == 0)
353 {
354 firstGraphicsQueueFamily = familyIndex;
355 }
356 break;
357 }
358 }
359
360 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
361
362 // If only one queue family, go ahead and initialize the device. If there is more than one
363 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
364 if (graphicsQueueFamilyCount == 1)
365 {
366 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
367 }
368
Jamie Madill035fd6b2017-10-03 15:43:22 -0400369 // Store the physical device memory properties so we can find the right memory pools.
370 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500371
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500372 mGlslangWrapper = GlslangWrapper::GetReference();
373
Jamie Madill6a89d222017-11-02 11:59:51 -0400374 // Initialize the format table.
375 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps);
376
Jamie Madill8c3988c2017-12-21 14:44:56 -0500377 // Initialize the pipeline layout for GL programs.
378 ANGLE_TRY(initGraphicsPipelineLayout());
379
Jamie Madill327ba852016-11-30 12:38:28 -0500380 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400381}
382
Jamie Madill4d0bf552016-12-28 15:45:24 -0500383vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
384{
385 uint32_t deviceLayerCount = 0;
386 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
387
388 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
389 if (deviceLayerCount > 0)
390 {
391 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
392 deviceLayerProps.data()));
393 }
394
395 uint32_t deviceExtensionCount = 0;
396 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
397 &deviceExtensionCount, nullptr));
398
399 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
400 if (deviceExtensionCount > 0)
401 {
402 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
403 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
404 }
405
406 if (mEnableValidationLayers)
407 {
408 if (!HasStandardValidationLayer(deviceLayerProps))
409 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500410 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500411 mEnableValidationLayers = false;
412 }
413 }
414
415 std::vector<const char *> enabledDeviceExtensions;
416 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
417
418 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
419
420 VkDeviceQueueCreateInfo queueCreateInfo;
421
422 float zeroPriority = 0.0f;
423
424 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
425 queueCreateInfo.pNext = nullptr;
426 queueCreateInfo.flags = 0;
427 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
428 queueCreateInfo.queueCount = 1;
429 queueCreateInfo.pQueuePriorities = &zeroPriority;
430
431 // Initialize the device
432 VkDeviceCreateInfo createInfo;
433
434 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
435 createInfo.pNext = nullptr;
436 createInfo.flags = 0;
437 createInfo.queueCreateInfoCount = 1;
438 createInfo.pQueueCreateInfos = &queueCreateInfo;
439 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
440 createInfo.ppEnabledLayerNames =
441 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
442 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
443 createInfo.ppEnabledExtensionNames =
444 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
445 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
446
447 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
448
449 mCurrentQueueFamilyIndex = queueFamilyIndex;
450
451 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
452
453 // Initialize the command pool now that we know the queue family index.
454 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500455 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
456 commandPoolInfo.pNext = nullptr;
457 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500458 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
459
Jamie Madill5deea722017-02-16 10:44:46 -0500460 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500461
Jamie Madill4d0bf552016-12-28 15:45:24 -0500462 return vk::NoError();
463}
464
465vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
466{
467 // We've already initialized a device, and can't re-create it unless it's never been used.
468 // TODO(jmadill): Handle the re-creation case if necessary.
469 if (mDevice != VK_NULL_HANDLE)
470 {
471 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
472
473 // Check if the current device supports present on this surface.
474 VkBool32 supportsPresent = VK_FALSE;
475 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
476 surface, &supportsPresent));
477
478 return (supportsPresent == VK_TRUE);
479 }
480
481 // Find a graphics and present queue.
482 Optional<uint32_t> newPresentQueue;
483 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
484 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
485 {
486 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
487 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
488 {
489 VkBool32 supportsPresent = VK_FALSE;
490 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
491 &supportsPresent));
492
493 if (supportsPresent == VK_TRUE)
494 {
495 newPresentQueue = queueIndex;
496 break;
497 }
498 }
499 }
500
501 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
502 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
503
504 return newPresentQueue.value();
505}
506
507std::string RendererVk::getVendorString() const
508{
509 switch (mPhysicalDeviceProperties.vendorID)
510 {
511 case VENDOR_ID_AMD:
512 return "Advanced Micro Devices";
513 case VENDOR_ID_NVIDIA:
514 return "NVIDIA";
515 case VENDOR_ID_INTEL:
516 return "Intel";
517 default:
518 {
519 // TODO(jmadill): More vendor IDs.
520 std::stringstream strstr;
521 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
522 return strstr.str();
523 }
524 }
525}
526
Jamie Madille09bd5d2016-11-29 16:20:35 -0500527std::string RendererVk::getRendererDescription() const
528{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500529 std::stringstream strstr;
530
531 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
532
533 strstr << "Vulkan ";
534 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
535 strstr << VK_VERSION_MINOR(apiVersion) << ".";
536 strstr << VK_VERSION_PATCH(apiVersion);
537
538 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
539
540 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500541}
542
Jamie Madillacccc6c2016-05-03 17:22:10 -0400543void RendererVk::ensureCapsInitialized() const
544{
545 if (!mCapsInitialized)
546 {
547 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
548 mCapsInitialized = true;
549 }
550}
551
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500552void RendererVk::generateCaps(gl::Caps *outCaps,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400553 gl::TextureCapsMap * /*outTextureCaps*/,
Jamie Madillb8353b02017-01-25 12:57:21 -0800554 gl::Extensions *outExtensions,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400555 gl::Limitations * /* outLimitations */) const
556{
Jamie Madill327ba852016-11-30 12:38:28 -0500557 // TODO(jmadill): Caps.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500558 outCaps->maxDrawBuffers = 1;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800559 outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS;
560 outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS;
Jamie Madill035fd6b2017-10-03 15:43:22 -0400561 outCaps->maxVaryingVectors = 16;
562 outCaps->maxTextureImageUnits = 1;
563 outCaps->maxCombinedTextureImageUnits = 1;
564 outCaps->max2DTextureSize = 1024;
Jamie Madilld03a8492017-10-03 15:46:06 -0400565 outCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
Jamie Madill6276b922017-09-25 02:35:57 -0400566 outCaps->maxFragmentUniformVectors = 8;
567 outCaps->maxVertexUniformVectors = 8;
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400568 outCaps->maxColorAttachments = 1;
Jamie Madillb8353b02017-01-25 12:57:21 -0800569
570 // Enable this for simple buffer readback testing, but some functionality is missing.
571 // TODO(jmadill): Support full mapBufferRange extension.
572 outExtensions->mapBuffer = true;
573 outExtensions->mapBufferRange = true;
Jamie Madillacccc6c2016-05-03 17:22:10 -0400574}
575
576const gl::Caps &RendererVk::getNativeCaps() const
577{
578 ensureCapsInitialized();
579 return mNativeCaps;
580}
581
582const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
583{
584 ensureCapsInitialized();
585 return mNativeTextureCaps;
586}
587
588const gl::Extensions &RendererVk::getNativeExtensions() const
589{
590 ensureCapsInitialized();
591 return mNativeExtensions;
592}
593
594const gl::Limitations &RendererVk::getNativeLimitations() const
595{
596 ensureCapsInitialized();
597 return mNativeLimitations;
598}
599
Jamie Madill49ac74b2017-12-21 14:42:33 -0500600const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500601{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500602 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603}
604
Jamie Madill49ac74b2017-12-21 14:42:33 -0500605vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500606{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500607 if (!mOpenCommandGraph.empty())
608 {
609 vk::CommandBuffer commandBatch;
610 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400611
Jamie Madill49ac74b2017-12-21 14:42:33 -0500612 VkSubmitInfo submitInfo;
613 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
614 submitInfo.pNext = nullptr;
615 submitInfo.waitSemaphoreCount = 0;
616 submitInfo.pWaitSemaphores = nullptr;
617 submitInfo.pWaitDstStageMask = nullptr;
618 submitInfo.commandBufferCount = 1;
619 submitInfo.pCommandBuffers = commandBatch.ptr();
620 submitInfo.signalSemaphoreCount = 0;
621 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500622
Jamie Madill49ac74b2017-12-21 14:42:33 -0500623 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
624 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500625
Jamie Madill4c26fc22017-02-24 11:04:10 -0500626 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500627 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400628 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500629 return vk::NoError();
630}
631
Jamie Madill0c0dc342017-03-24 14:18:51 -0400632void RendererVk::freeAllInFlightResources()
633{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500634 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400635 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500636 batch.fence.destroy(mDevice);
637 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400638 }
639 mInFlightCommands.clear();
640
641 for (auto &garbage : mGarbage)
642 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400643 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400644 }
645 mGarbage.clear();
646}
647
Jamie Madill4c26fc22017-02-24 11:04:10 -0500648vk::Error RendererVk::checkInFlightCommands()
649{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500650 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500651
Jamie Madill49ac74b2017-12-21 14:42:33 -0500652 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500653 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500654 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400655 if (result == VK_NOT_READY)
656 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500657
Jamie Madill0c0dc342017-03-24 14:18:51 -0400658 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500659 ASSERT(batch.serial > mLastCompletedQueueSerial);
660 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400661
Jamie Madill49ac74b2017-12-21 14:42:33 -0500662 batch.fence.destroy(mDevice);
663 batch.commandPool.destroy(mDevice);
664 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500665 }
666
Jamie Madill49ac74b2017-12-21 14:42:33 -0500667 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400668
669 size_t freeIndex = 0;
670 for (; freeIndex < mGarbage.size(); ++freeIndex)
671 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500672 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400673 break;
674 }
675
676 // Remove the entries from the garbage list - they should be ready to go.
677 if (freeIndex > 0)
678 {
679 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500680 }
681
Jamie Madill4c26fc22017-02-24 11:04:10 -0500682 return vk::NoError();
683}
684
Jamie Madill49ac74b2017-12-21 14:42:33 -0500685vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500686{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500687 VkFenceCreateInfo fenceInfo;
688 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
689 fenceInfo.pNext = nullptr;
690 fenceInfo.flags = 0;
691
692 CommandBatch batch;
693 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
694
695 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500696
697 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500698 batch.commandPool = std::move(mCommandPool);
699 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500700
Jamie Madill49ac74b2017-12-21 14:42:33 -0500701 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400702
703 // Sanity check.
704 ASSERT(mInFlightCommands.size() < 1000u);
705
706 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400707 // TODO(jmadill): Overflow check.
708 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400709
710 ANGLE_TRY(checkInFlightCommands());
711
Jamie Madill49ac74b2017-12-21 14:42:33 -0500712 // Simply null out the command buffer here - it was allocated using the command pool.
713 commandBuffer.releaseHandle();
714
715 // Reallocate the command pool for next frame.
716 // TODO(jmadill): Consider reusing command pools.
717 VkCommandPoolCreateInfo poolInfo;
718 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
719 poolInfo.pNext = nullptr;
720 poolInfo.flags = 0;
721 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
722
723 mCommandPool.init(mDevice, poolInfo);
724
Jamie Madill4c26fc22017-02-24 11:04:10 -0500725 return vk::NoError();
726}
727
Jamie Madill5deea722017-02-16 10:44:46 -0500728vk::Error RendererVk::createStagingImage(TextureDimension dimension,
729 const vk::Format &format,
730 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400731 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500732 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500733{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400734 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
Jamie Madill1d7be502017-10-29 18:06:50 -0400735 format.vkTextureFormat, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500736 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500737}
738
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500739GlslangWrapper *RendererVk::getGlslangWrapper()
740{
741 return mGlslangWrapper;
742}
743
Jamie Madill4c26fc22017-02-24 11:04:10 -0500744Serial RendererVk::getCurrentQueueSerial() const
745{
746 return mCurrentQueueSerial;
747}
748
Jamie Madill97760352017-11-09 13:08:29 -0500749bool RendererVk::isResourceInUse(const ResourceVk &resource)
750{
751 return isSerialInUse(resource.getQueueSerial());
752}
753
754bool RendererVk::isSerialInUse(Serial serial)
755{
756 return serial > mLastCompletedQueueSerial;
757}
758
Jamie Madill9f2a8612017-11-30 12:43:09 -0500759vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
760 vk::RenderPass **renderPassOut)
761{
762 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
763 renderPassOut);
764}
765
Jamie Madillbef918c2017-12-13 13:11:30 -0500766vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
767 const vk::AttachmentOpsArray &ops,
768 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500769{
Jamie Madillbef918c2017-12-13 13:11:30 -0500770 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
771 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500772}
773
Jamie Madill49ac74b2017-12-21 14:42:33 -0500774vk::CommandBufferNode *RendererVk::allocateCommandNode()
775{
776 // TODO(jmadill): Use a pool allocator for the CPU node allocations.
777 vk::CommandBufferNode *newCommands = new vk::CommandBufferNode();
778 mOpenCommandGraph.emplace_back(newCommands);
779 return newCommands;
780}
781
782vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
783{
784 VkCommandBufferAllocateInfo primaryInfo;
785 primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
786 primaryInfo.pNext = nullptr;
787 primaryInfo.commandPool = mCommandPool.getHandle();
788 primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
789 primaryInfo.commandBufferCount = 1;
790
791 ANGLE_TRY(commandBatch->init(mDevice, primaryInfo));
792
793 if (mOpenCommandGraph.empty())
794 {
795 return vk::NoError();
796 }
797
798 std::vector<vk::CommandBufferNode *> nodeStack;
799
800 VkCommandBufferBeginInfo beginInfo;
801 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
802 beginInfo.pNext = nullptr;
803 beginInfo.flags = 0;
804 beginInfo.pInheritanceInfo = nullptr;
805
806 ANGLE_TRY(commandBatch->begin(beginInfo));
807
808 for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph)
809 {
810 // Only process commands that don't have child commands. The others will be pulled in
811 // automatically. Also skip commands that have already been visited.
812 if (topLevelNode->isDependency() ||
813 topLevelNode->visitedState() != vk::VisitedState::Unvisited)
814 continue;
815
816 nodeStack.push_back(topLevelNode);
817
818 while (!nodeStack.empty())
819 {
820 vk::CommandBufferNode *node = nodeStack.back();
821
822 switch (node->visitedState())
823 {
824 case vk::VisitedState::Unvisited:
825 node->visitDependencies(&nodeStack);
826 break;
827 case vk::VisitedState::Ready:
828 ANGLE_TRY(node->visitAndExecute(this, commandBatch));
829 nodeStack.pop_back();
830 break;
831 case vk::VisitedState::Visited:
832 nodeStack.pop_back();
833 break;
834 default:
835 UNREACHABLE();
836 break;
837 }
838 }
839 }
840
841 ANGLE_TRY(commandBatch->end());
Jamie Madill97f39b32018-01-05 13:14:29 -0500842 resetCommandGraph();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500843 return vk::NoError();
844}
845
846void RendererVk::resetCommandGraph()
847{
848 // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph.
849 for (vk::CommandBufferNode *node : mOpenCommandGraph)
850 {
851 delete node;
852 }
853 mOpenCommandGraph.clear();
854}
855
856vk::Error RendererVk::flush(const gl::Context *context,
857 const vk::Semaphore &waitSemaphore,
858 const vk::Semaphore &signalSemaphore)
859{
860 vk::CommandBuffer commandBatch;
861 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
862
863 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
864
865 VkSubmitInfo submitInfo;
866 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
867 submitInfo.pNext = nullptr;
868 submitInfo.waitSemaphoreCount = 1;
869 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
870 submitInfo.pWaitDstStageMask = &waitStageMask;
871 submitInfo.commandBufferCount = 1;
872 submitInfo.pCommandBuffers = commandBatch.ptr();
873 submitInfo.signalSemaphoreCount = 1;
874 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
875
876 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
877 return vk::NoError();
878}
879
Jamie Madill8c3988c2017-12-21 14:44:56 -0500880const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
881{
882 return mGraphicsPipelineLayout;
883}
884
885const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
886{
887 return mGraphicsDescriptorSetLayouts;
888}
889
890vk::Error RendererVk::initGraphicsPipelineLayout()
891{
892 ASSERT(!mGraphicsPipelineLayout.valid());
893
894 // Create two descriptor set layouts: one for default uniform info, and one for textures.
895 // Skip one or both if there are no uniforms.
896 VkDescriptorSetLayoutBinding uniformBindings[2];
897 uint32_t blockCount = 0;
898
899 {
900 auto &layoutBinding = uniformBindings[blockCount];
901
902 layoutBinding.binding = blockCount;
903 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
904 layoutBinding.descriptorCount = 1;
905 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
906 layoutBinding.pImmutableSamplers = nullptr;
907
908 blockCount++;
909 }
910
911 {
912 auto &layoutBinding = uniformBindings[blockCount];
913
914 layoutBinding.binding = blockCount;
915 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
916 layoutBinding.descriptorCount = 1;
917 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
918 layoutBinding.pImmutableSamplers = nullptr;
919
920 blockCount++;
921 }
922
923 {
924 VkDescriptorSetLayoutCreateInfo uniformInfo;
925 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
926 uniformInfo.pNext = nullptr;
927 uniformInfo.flags = 0;
928 uniformInfo.bindingCount = blockCount;
929 uniformInfo.pBindings = uniformBindings;
930
931 vk::DescriptorSetLayout uniformLayout;
932 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
933 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
934 }
935
Yuly Novikov37968132018-01-23 18:19:29 -0500936 // TODO(lucferron): expose this limitation to GL in Context Caps
937 std::vector<VkDescriptorSetLayoutBinding> textureBindings(
938 std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
939 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES));
Jamie Madill8c3988c2017-12-21 14:44:56 -0500940
941 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500942 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500943 {
944 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
945
946 layoutBinding.binding = textureIndex;
947 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
948 layoutBinding.descriptorCount = 1;
949 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
950 layoutBinding.pImmutableSamplers = nullptr;
951 }
952
953 {
954 VkDescriptorSetLayoutCreateInfo textureInfo;
955 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
956 textureInfo.pNext = nullptr;
957 textureInfo.flags = 0;
958 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
959 textureInfo.pBindings = textureBindings.data();
960
961 vk::DescriptorSetLayout textureLayout;
962 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
963 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
964 }
965
966 VkPipelineLayoutCreateInfo createInfo;
967 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
968 createInfo.pNext = nullptr;
969 createInfo.flags = 0;
970 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
971 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
972 createInfo.pushConstantRangeCount = 0;
973 createInfo.pPushConstantRanges = nullptr;
974
975 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
976
977 return vk::NoError();
978}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500979
980Serial RendererVk::issueProgramSerial()
981{
982 return mProgramSerialFactory.generate();
983}
984
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400985} // namespace rx