blob: 1847f4068f326f0dcf9029f44debe2eabc40354c [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
2// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// RendererVk.cpp:
7// Implements the class methods for RendererVk.
8//
9
10#include "libANGLE/renderer/vulkan/RendererVk.h"
11
Jamie Madill4d0bf552016-12-28 15:45:24 -050012// Placing this first seems to solve an intellisense bug.
Jamie Madill3c424b42018-01-19 12:35:09 -050013#include "libANGLE/renderer/vulkan/vk_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050014
Jamie Madille09bd5d2016-11-29 16:20:35 -050015#include <EGL/eglext.h>
16
Jamie Madill9e54b5a2016-05-25 12:57:39 -040017#include "common/debug.h"
Jamie Madilla66779f2017-01-06 10:43:44 -050018#include "common/system_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050019#include "libANGLE/renderer/driver_utils.h"
Jamie Madill49ac74b2017-12-21 14:42:33 -050020#include "libANGLE/renderer/vulkan/CommandBufferNode.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050021#include "libANGLE/renderer/vulkan/CompilerVk.h"
22#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050023#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050024#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050025#include "libANGLE/renderer/vulkan/TextureVk.h"
26#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050027#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050028#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050029#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040030
31namespace rx
32{
33
Jamie Madille09bd5d2016-11-29 16:20:35 -050034namespace
35{
36
37VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
38 const std::vector<const char *> &enabledExtensionNames)
39{
40 // Compile the extensions names into a set.
41 std::set<std::string> extensionNames;
42 for (const auto &extensionProp : extensionProps)
43 {
44 extensionNames.insert(extensionProp.extensionName);
45 }
46
Jamie Madillacf2f3a2017-11-21 19:22:44 -050047 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050048 {
49 if (extensionNames.count(extensionName) == 0)
50 {
51 return VK_ERROR_EXTENSION_NOT_PRESENT;
52 }
53 }
54
55 return VK_SUCCESS;
56}
57
Yuly Novikov199f4292018-01-19 19:04:05 -050058VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
59 VkDebugReportObjectTypeEXT objectType,
60 uint64_t object,
61 size_t location,
62 int32_t messageCode,
63 const char *layerPrefix,
64 const char *message,
65 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050066{
67 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
68 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050069 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050070#if !defined(NDEBUG)
71 // Abort the call in Debug builds.
72 return VK_TRUE;
73#endif
74 }
75 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
76 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050077 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050078 }
79 else
80 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050081 // Uncomment this if you want Vulkan spam.
82 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050083 }
84
85 return VK_FALSE;
86}
87
Yuly Novikov199f4292018-01-19 19:04:05 -050088// If we're loading the validation layers, we could be running from any random directory.
89// Change to the executable directory so we can find the layers, then change back to the
90// previous directory to be safe we don't disrupt the application.
91class ScopedVkLoaderEnvironment : angle::NonCopyable
92{
93 public:
94 ScopedVkLoaderEnvironment(bool enableValidationLayers)
95 : mEnableValidationLayers(enableValidationLayers), mChangedCWD(false)
96 {
97// Changing CWD and setting environment variables makes no sense on Android,
98// since this code is a part of Java application there.
99// Android Vulkan loader doesn't need this either.
100#if !defined(ANGLE_PLATFORM_ANDROID)
101 if (mEnableValidationLayers)
102 {
103 const auto &cwd = angle::GetCWD();
104 if (!cwd.valid())
105 {
106 ERR() << "Error getting CWD for Vulkan layers init.";
107 mEnableValidationLayers = false;
108 }
109 else
110 {
111 mPreviousCWD = cwd.value();
112 const char *exeDir = angle::GetExecutableDirectory();
113 mChangedCWD = angle::SetCWD(exeDir);
114 if (!mChangedCWD)
115 {
116 ERR() << "Error setting CWD for Vulkan layers init.";
117 mEnableValidationLayers = false;
118 }
119 }
120 }
121
122 // Override environment variable to use the ANGLE layers.
123 if (mEnableValidationLayers)
124 {
125 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
126 {
127 ERR() << "Error setting environment for Vulkan layers init.";
128 mEnableValidationLayers = false;
129 }
130 }
131#endif // !defined(ANGLE_PLATFORM_ANDROID)
132 }
133
134 ~ScopedVkLoaderEnvironment()
135 {
136 if (mChangedCWD)
137 {
138#if !defined(ANGLE_PLATFORM_ANDROID)
139 ASSERT(mPreviousCWD.valid());
140 angle::SetCWD(mPreviousCWD.value().c_str());
141#endif // !defined(ANGLE_PLATFORM_ANDROID)
142 }
143 }
144
145 bool canEnableValidationLayers() { return mEnableValidationLayers; }
146
147 private:
148 bool mEnableValidationLayers;
149 bool mChangedCWD;
150 Optional<std::string> mPreviousCWD;
151};
152
Jamie Madille09bd5d2016-11-29 16:20:35 -0500153} // anonymous namespace
154
Jamie Madill49ac74b2017-12-21 14:42:33 -0500155// CommandBatch implementation.
156RendererVk::CommandBatch::CommandBatch()
157{
158}
159
160RendererVk::CommandBatch::~CommandBatch()
161{
162}
163
164RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
165 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
166{
167}
168
169RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
170{
171 std::swap(commandPool, other.commandPool);
172 std::swap(fence, other.fence);
173 std::swap(serial, other.serial);
174 return *this;
175}
176
Jamie Madill9f2a8612017-11-30 12:43:09 -0500177// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500178RendererVk::RendererVk()
179 : mCapsInitialized(false),
180 mInstance(VK_NULL_HANDLE),
181 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500182 mDebugReportCallback(VK_NULL_HANDLE),
183 mPhysicalDevice(VK_NULL_HANDLE),
184 mQueue(VK_NULL_HANDLE),
185 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
186 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500187 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400188 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
189 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500190 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400191{
192}
193
194RendererVk::~RendererVk()
195{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500196 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500197 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500198 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
199 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500200 if (error.isError())
201 {
202 ERR() << "Error during VK shutdown: " << error;
203 }
204 }
205
Jamie Madill8c3988c2017-12-21 14:44:56 -0500206 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
207 {
208 descriptorSetLayout.destroy(mDevice);
209 }
210
211 mGraphicsPipelineLayout.destroy(mDevice);
212
Jamie Madill9f2a8612017-11-30 12:43:09 -0500213 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500214 mPipelineCache.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500215
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500216 if (mGlslangWrapper)
217 {
218 GlslangWrapper::ReleaseReference();
219 mGlslangWrapper = nullptr;
220 }
221
Jamie Madill5deea722017-02-16 10:44:46 -0500222 if (mCommandPool.valid())
223 {
224 mCommandPool.destroy(mDevice);
225 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500226
227 if (mDevice)
228 {
229 vkDestroyDevice(mDevice, nullptr);
230 mDevice = VK_NULL_HANDLE;
231 }
232
Jamie Madill0448ec82016-12-23 13:41:47 -0500233 if (mDebugReportCallback)
234 {
235 ASSERT(mInstance);
236 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
237 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
238 ASSERT(destroyDebugReportCallback);
239 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
240 }
241
Jamie Madill4d0bf552016-12-28 15:45:24 -0500242 if (mInstance)
243 {
244 vkDestroyInstance(mInstance, nullptr);
245 mInstance = VK_NULL_HANDLE;
246 }
247
248 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500249}
250
Frank Henigman29f148b2016-11-23 21:05:36 -0500251vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500252{
Jamie Madill222c5172017-07-19 16:15:42 -0400253 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500254
Yuly Novikov199f4292018-01-19 19:04:05 -0500255 ScopedVkLoaderEnvironment scopedEnvironment(mEnableValidationLayers);
256 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madilla66779f2017-01-06 10:43:44 -0500257
Jamie Madill0448ec82016-12-23 13:41:47 -0500258 // Gather global layer properties.
259 uint32_t instanceLayerCount = 0;
260 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
261
262 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
263 if (instanceLayerCount > 0)
264 {
265 ANGLE_VK_TRY(
266 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
267 }
268
Jamie Madille09bd5d2016-11-29 16:20:35 -0500269 uint32_t instanceExtensionCount = 0;
270 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
271
272 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
273 if (instanceExtensionCount > 0)
274 {
275 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
276 instanceExtensionProps.data()));
277 }
278
Yuly Novikov199f4292018-01-19 19:04:05 -0500279 const char *const *enabledLayerNames = nullptr;
280 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500281 if (mEnableValidationLayers)
282 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500283 bool layersRequested =
284 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
285 mEnableValidationLayers = GetAvailableValidationLayers(
286 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500287 }
288
Jamie Madille09bd5d2016-11-29 16:20:35 -0500289 std::vector<const char *> enabledInstanceExtensions;
290 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500291 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500292
Jamie Madill0448ec82016-12-23 13:41:47 -0500293 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
294 if (mEnableValidationLayers)
295 {
296 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
297 }
298
Jamie Madille09bd5d2016-11-29 16:20:35 -0500299 // Verify the required extensions are in the extension names set. Fail if not.
300 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
301
Jamie Madill327ba852016-11-30 12:38:28 -0500302 VkApplicationInfo applicationInfo;
303 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
304 applicationInfo.pNext = nullptr;
305 applicationInfo.pApplicationName = "ANGLE";
306 applicationInfo.applicationVersion = 1;
307 applicationInfo.pEngineName = "ANGLE";
308 applicationInfo.engineVersion = 1;
309 applicationInfo.apiVersion = VK_API_VERSION_1_0;
310
311 VkInstanceCreateInfo instanceInfo;
312 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
313 instanceInfo.pNext = nullptr;
314 instanceInfo.flags = 0;
315 instanceInfo.pApplicationInfo = &applicationInfo;
316
Jamie Madille09bd5d2016-11-29 16:20:35 -0500317 // Enable requested layers and extensions.
318 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
319 instanceInfo.ppEnabledExtensionNames =
320 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500321 instanceInfo.enabledLayerCount = enabledLayerCount;
322 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500323
324 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
325
Jamie Madill0448ec82016-12-23 13:41:47 -0500326 if (mEnableValidationLayers)
327 {
328 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
329
330 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
331 debugReportInfo.pNext = nullptr;
332 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
333 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
334 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
335 debugReportInfo.pfnCallback = &DebugReportCallback;
336 debugReportInfo.pUserData = this;
337
338 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
339 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
340 ASSERT(createDebugReportCallback);
341 ANGLE_VK_TRY(
342 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
343 }
344
Jamie Madill4d0bf552016-12-28 15:45:24 -0500345 uint32_t physicalDeviceCount = 0;
346 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
347 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
348
349 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
350 physicalDeviceCount = 1;
351 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
352
353 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
354
355 // Ensure we can find a graphics queue family.
356 uint32_t queueCount = 0;
357 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
358
359 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
360
361 mQueueFamilyProperties.resize(queueCount);
362 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
363 mQueueFamilyProperties.data());
364
365 size_t graphicsQueueFamilyCount = false;
366 uint32_t firstGraphicsQueueFamily = 0;
367 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
368 {
369 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
370 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
371 {
372 ASSERT(queueInfo.queueCount > 0);
373 graphicsQueueFamilyCount++;
374 if (firstGraphicsQueueFamily == 0)
375 {
376 firstGraphicsQueueFamily = familyIndex;
377 }
378 break;
379 }
380 }
381
382 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
383
384 // If only one queue family, go ahead and initialize the device. If there is more than one
385 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
386 if (graphicsQueueFamilyCount == 1)
387 {
388 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
389 }
390
Jamie Madill035fd6b2017-10-03 15:43:22 -0400391 // Store the physical device memory properties so we can find the right memory pools.
392 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500393
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500394 mGlslangWrapper = GlslangWrapper::GetReference();
395
Jamie Madill6a89d222017-11-02 11:59:51 -0400396 // Initialize the format table.
397 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps);
398
Jamie Madill8c3988c2017-12-21 14:44:56 -0500399 // Initialize the pipeline layout for GL programs.
400 ANGLE_TRY(initGraphicsPipelineLayout());
401
Jamie Madill327ba852016-11-30 12:38:28 -0500402 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400403}
404
Jamie Madill4d0bf552016-12-28 15:45:24 -0500405vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
406{
407 uint32_t deviceLayerCount = 0;
408 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
409
410 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
411 if (deviceLayerCount > 0)
412 {
413 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
414 deviceLayerProps.data()));
415 }
416
417 uint32_t deviceExtensionCount = 0;
418 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
419 &deviceExtensionCount, nullptr));
420
421 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
422 if (deviceExtensionCount > 0)
423 {
424 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
425 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
426 }
427
Yuly Novikov199f4292018-01-19 19:04:05 -0500428 const char *const *enabledLayerNames = nullptr;
429 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500430 if (mEnableValidationLayers)
431 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500432 mEnableValidationLayers = GetAvailableValidationLayers(
433 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500434 }
435
436 std::vector<const char *> enabledDeviceExtensions;
437 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
438
439 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
440
441 VkDeviceQueueCreateInfo queueCreateInfo;
442
443 float zeroPriority = 0.0f;
444
445 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
446 queueCreateInfo.pNext = nullptr;
447 queueCreateInfo.flags = 0;
448 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
449 queueCreateInfo.queueCount = 1;
450 queueCreateInfo.pQueuePriorities = &zeroPriority;
451
452 // Initialize the device
453 VkDeviceCreateInfo createInfo;
454
455 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
456 createInfo.pNext = nullptr;
457 createInfo.flags = 0;
458 createInfo.queueCreateInfoCount = 1;
459 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500460 createInfo.enabledLayerCount = enabledLayerCount;
461 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500462 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
463 createInfo.ppEnabledExtensionNames =
464 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
465 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
466
467 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
468
469 mCurrentQueueFamilyIndex = queueFamilyIndex;
470
471 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
472
473 // Initialize the command pool now that we know the queue family index.
474 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500475 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
476 commandPoolInfo.pNext = nullptr;
477 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500478 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
479
Jamie Madill5deea722017-02-16 10:44:46 -0500480 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500481
Jamie Madill4d0bf552016-12-28 15:45:24 -0500482 return vk::NoError();
483}
484
485vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
486{
487 // We've already initialized a device, and can't re-create it unless it's never been used.
488 // TODO(jmadill): Handle the re-creation case if necessary.
489 if (mDevice != VK_NULL_HANDLE)
490 {
491 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
492
493 // Check if the current device supports present on this surface.
494 VkBool32 supportsPresent = VK_FALSE;
495 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
496 surface, &supportsPresent));
497
498 return (supportsPresent == VK_TRUE);
499 }
500
501 // Find a graphics and present queue.
502 Optional<uint32_t> newPresentQueue;
503 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
504 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
505 {
506 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
507 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
508 {
509 VkBool32 supportsPresent = VK_FALSE;
510 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
511 &supportsPresent));
512
513 if (supportsPresent == VK_TRUE)
514 {
515 newPresentQueue = queueIndex;
516 break;
517 }
518 }
519 }
520
521 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
522 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
523
524 return newPresentQueue.value();
525}
526
527std::string RendererVk::getVendorString() const
528{
529 switch (mPhysicalDeviceProperties.vendorID)
530 {
531 case VENDOR_ID_AMD:
532 return "Advanced Micro Devices";
533 case VENDOR_ID_NVIDIA:
534 return "NVIDIA";
535 case VENDOR_ID_INTEL:
536 return "Intel";
537 default:
538 {
539 // TODO(jmadill): More vendor IDs.
540 std::stringstream strstr;
541 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
542 return strstr.str();
543 }
544 }
545}
546
Jamie Madille09bd5d2016-11-29 16:20:35 -0500547std::string RendererVk::getRendererDescription() const
548{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500549 std::stringstream strstr;
550
551 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
552
553 strstr << "Vulkan ";
554 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
555 strstr << VK_VERSION_MINOR(apiVersion) << ".";
556 strstr << VK_VERSION_PATCH(apiVersion);
557
558 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
559
560 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500561}
562
Jamie Madillacccc6c2016-05-03 17:22:10 -0400563void RendererVk::ensureCapsInitialized() const
564{
565 if (!mCapsInitialized)
566 {
Luc Ferrone4741fd2018-01-25 13:25:27 -0500567 vk::GenerateCaps(mPhysicalDeviceProperties, &mNativeCaps, &mNativeTextureCaps,
568 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400569 mCapsInitialized = true;
570 }
571}
572
Jamie Madillacccc6c2016-05-03 17:22:10 -0400573const gl::Caps &RendererVk::getNativeCaps() const
574{
575 ensureCapsInitialized();
576 return mNativeCaps;
577}
578
579const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
580{
581 ensureCapsInitialized();
582 return mNativeTextureCaps;
583}
584
585const gl::Extensions &RendererVk::getNativeExtensions() const
586{
587 ensureCapsInitialized();
588 return mNativeExtensions;
589}
590
591const gl::Limitations &RendererVk::getNativeLimitations() const
592{
593 ensureCapsInitialized();
594 return mNativeLimitations;
595}
596
Jamie Madill49ac74b2017-12-21 14:42:33 -0500597const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500598{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500599 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500600}
601
Jamie Madill49ac74b2017-12-21 14:42:33 -0500602vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500604 if (!mOpenCommandGraph.empty())
605 {
606 vk::CommandBuffer commandBatch;
607 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400608
Jamie Madill49ac74b2017-12-21 14:42:33 -0500609 VkSubmitInfo submitInfo;
610 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
611 submitInfo.pNext = nullptr;
612 submitInfo.waitSemaphoreCount = 0;
613 submitInfo.pWaitSemaphores = nullptr;
614 submitInfo.pWaitDstStageMask = nullptr;
615 submitInfo.commandBufferCount = 1;
616 submitInfo.pCommandBuffers = commandBatch.ptr();
617 submitInfo.signalSemaphoreCount = 0;
618 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500619
Jamie Madill49ac74b2017-12-21 14:42:33 -0500620 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
621 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500622
Jamie Madill4c26fc22017-02-24 11:04:10 -0500623 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500624 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400625 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500626 return vk::NoError();
627}
628
Jamie Madill0c0dc342017-03-24 14:18:51 -0400629void RendererVk::freeAllInFlightResources()
630{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500631 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400632 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500633 batch.fence.destroy(mDevice);
634 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400635 }
636 mInFlightCommands.clear();
637
638 for (auto &garbage : mGarbage)
639 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400640 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400641 }
642 mGarbage.clear();
643}
644
Jamie Madill4c26fc22017-02-24 11:04:10 -0500645vk::Error RendererVk::checkInFlightCommands()
646{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500647 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500648
Jamie Madill49ac74b2017-12-21 14:42:33 -0500649 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500650 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500651 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400652 if (result == VK_NOT_READY)
653 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500654
Jamie Madill0c0dc342017-03-24 14:18:51 -0400655 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500656 ASSERT(batch.serial > mLastCompletedQueueSerial);
657 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400658
Jamie Madill49ac74b2017-12-21 14:42:33 -0500659 batch.fence.destroy(mDevice);
660 batch.commandPool.destroy(mDevice);
661 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500662 }
663
Jamie Madill49ac74b2017-12-21 14:42:33 -0500664 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400665
666 size_t freeIndex = 0;
667 for (; freeIndex < mGarbage.size(); ++freeIndex)
668 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500669 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400670 break;
671 }
672
673 // Remove the entries from the garbage list - they should be ready to go.
674 if (freeIndex > 0)
675 {
676 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500677 }
678
Jamie Madill4c26fc22017-02-24 11:04:10 -0500679 return vk::NoError();
680}
681
Jamie Madill49ac74b2017-12-21 14:42:33 -0500682vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500683{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500684 VkFenceCreateInfo fenceInfo;
685 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
686 fenceInfo.pNext = nullptr;
687 fenceInfo.flags = 0;
688
689 CommandBatch batch;
690 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
691
692 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500693
694 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500695 batch.commandPool = std::move(mCommandPool);
696 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500697
Jamie Madill49ac74b2017-12-21 14:42:33 -0500698 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400699
700 // Sanity check.
701 ASSERT(mInFlightCommands.size() < 1000u);
702
703 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400704 // TODO(jmadill): Overflow check.
705 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400706
707 ANGLE_TRY(checkInFlightCommands());
708
Jamie Madill49ac74b2017-12-21 14:42:33 -0500709 // Simply null out the command buffer here - it was allocated using the command pool.
710 commandBuffer.releaseHandle();
711
712 // Reallocate the command pool for next frame.
713 // TODO(jmadill): Consider reusing command pools.
714 VkCommandPoolCreateInfo poolInfo;
715 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
716 poolInfo.pNext = nullptr;
717 poolInfo.flags = 0;
718 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
719
720 mCommandPool.init(mDevice, poolInfo);
721
Jamie Madill4c26fc22017-02-24 11:04:10 -0500722 return vk::NoError();
723}
724
Jamie Madill5deea722017-02-16 10:44:46 -0500725vk::Error RendererVk::createStagingImage(TextureDimension dimension,
726 const vk::Format &format,
727 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400728 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500729 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500730{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400731 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
Jamie Madill1d7be502017-10-29 18:06:50 -0400732 format.vkTextureFormat, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500733 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500734}
735
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500736GlslangWrapper *RendererVk::getGlslangWrapper()
737{
738 return mGlslangWrapper;
739}
740
Jamie Madill4c26fc22017-02-24 11:04:10 -0500741Serial RendererVk::getCurrentQueueSerial() const
742{
743 return mCurrentQueueSerial;
744}
745
Jamie Madill97760352017-11-09 13:08:29 -0500746bool RendererVk::isResourceInUse(const ResourceVk &resource)
747{
748 return isSerialInUse(resource.getQueueSerial());
749}
750
751bool RendererVk::isSerialInUse(Serial serial)
752{
753 return serial > mLastCompletedQueueSerial;
754}
755
Jamie Madill9f2a8612017-11-30 12:43:09 -0500756vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
757 vk::RenderPass **renderPassOut)
758{
759 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
760 renderPassOut);
761}
762
Jamie Madillbef918c2017-12-13 13:11:30 -0500763vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
764 const vk::AttachmentOpsArray &ops,
765 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500766{
Jamie Madillbef918c2017-12-13 13:11:30 -0500767 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
768 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500769}
770
Jamie Madill49ac74b2017-12-21 14:42:33 -0500771vk::CommandBufferNode *RendererVk::allocateCommandNode()
772{
773 // TODO(jmadill): Use a pool allocator for the CPU node allocations.
774 vk::CommandBufferNode *newCommands = new vk::CommandBufferNode();
775 mOpenCommandGraph.emplace_back(newCommands);
776 return newCommands;
777}
778
779vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
780{
781 VkCommandBufferAllocateInfo primaryInfo;
782 primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
783 primaryInfo.pNext = nullptr;
784 primaryInfo.commandPool = mCommandPool.getHandle();
785 primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
786 primaryInfo.commandBufferCount = 1;
787
788 ANGLE_TRY(commandBatch->init(mDevice, primaryInfo));
789
790 if (mOpenCommandGraph.empty())
791 {
792 return vk::NoError();
793 }
794
795 std::vector<vk::CommandBufferNode *> nodeStack;
796
797 VkCommandBufferBeginInfo beginInfo;
798 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
799 beginInfo.pNext = nullptr;
800 beginInfo.flags = 0;
801 beginInfo.pInheritanceInfo = nullptr;
802
803 ANGLE_TRY(commandBatch->begin(beginInfo));
804
805 for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph)
806 {
807 // Only process commands that don't have child commands. The others will be pulled in
808 // automatically. Also skip commands that have already been visited.
809 if (topLevelNode->isDependency() ||
810 topLevelNode->visitedState() != vk::VisitedState::Unvisited)
811 continue;
812
813 nodeStack.push_back(topLevelNode);
814
815 while (!nodeStack.empty())
816 {
817 vk::CommandBufferNode *node = nodeStack.back();
818
819 switch (node->visitedState())
820 {
821 case vk::VisitedState::Unvisited:
822 node->visitDependencies(&nodeStack);
823 break;
824 case vk::VisitedState::Ready:
825 ANGLE_TRY(node->visitAndExecute(this, commandBatch));
826 nodeStack.pop_back();
827 break;
828 case vk::VisitedState::Visited:
829 nodeStack.pop_back();
830 break;
831 default:
832 UNREACHABLE();
833 break;
834 }
835 }
836 }
837
838 ANGLE_TRY(commandBatch->end());
Jamie Madill97f39b32018-01-05 13:14:29 -0500839 resetCommandGraph();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500840 return vk::NoError();
841}
842
843void RendererVk::resetCommandGraph()
844{
845 // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph.
846 for (vk::CommandBufferNode *node : mOpenCommandGraph)
847 {
848 delete node;
849 }
850 mOpenCommandGraph.clear();
851}
852
853vk::Error RendererVk::flush(const gl::Context *context,
854 const vk::Semaphore &waitSemaphore,
855 const vk::Semaphore &signalSemaphore)
856{
857 vk::CommandBuffer commandBatch;
858 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
859
860 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
861
862 VkSubmitInfo submitInfo;
863 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
864 submitInfo.pNext = nullptr;
865 submitInfo.waitSemaphoreCount = 1;
866 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
867 submitInfo.pWaitDstStageMask = &waitStageMask;
868 submitInfo.commandBufferCount = 1;
869 submitInfo.pCommandBuffers = commandBatch.ptr();
870 submitInfo.signalSemaphoreCount = 1;
871 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
872
873 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
874 return vk::NoError();
875}
876
Jamie Madill8c3988c2017-12-21 14:44:56 -0500877const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
878{
879 return mGraphicsPipelineLayout;
880}
881
882const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
883{
884 return mGraphicsDescriptorSetLayouts;
885}
886
887vk::Error RendererVk::initGraphicsPipelineLayout()
888{
889 ASSERT(!mGraphicsPipelineLayout.valid());
890
891 // Create two descriptor set layouts: one for default uniform info, and one for textures.
892 // Skip one or both if there are no uniforms.
893 VkDescriptorSetLayoutBinding uniformBindings[2];
894 uint32_t blockCount = 0;
895
896 {
897 auto &layoutBinding = uniformBindings[blockCount];
898
899 layoutBinding.binding = blockCount;
900 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
901 layoutBinding.descriptorCount = 1;
902 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
903 layoutBinding.pImmutableSamplers = nullptr;
904
905 blockCount++;
906 }
907
908 {
909 auto &layoutBinding = uniformBindings[blockCount];
910
911 layoutBinding.binding = blockCount;
912 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
913 layoutBinding.descriptorCount = 1;
914 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
915 layoutBinding.pImmutableSamplers = nullptr;
916
917 blockCount++;
918 }
919
920 {
921 VkDescriptorSetLayoutCreateInfo uniformInfo;
922 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
923 uniformInfo.pNext = nullptr;
924 uniformInfo.flags = 0;
925 uniformInfo.bindingCount = blockCount;
926 uniformInfo.pBindings = uniformBindings;
927
928 vk::DescriptorSetLayout uniformLayout;
929 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
930 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
931 }
932
Yuly Novikov37968132018-01-23 18:19:29 -0500933 // TODO(lucferron): expose this limitation to GL in Context Caps
934 std::vector<VkDescriptorSetLayoutBinding> textureBindings(
935 std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
936 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES));
Jamie Madill8c3988c2017-12-21 14:44:56 -0500937
938 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500939 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500940 {
941 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
942
943 layoutBinding.binding = textureIndex;
944 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
945 layoutBinding.descriptorCount = 1;
946 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
947 layoutBinding.pImmutableSamplers = nullptr;
948 }
949
950 {
951 VkDescriptorSetLayoutCreateInfo textureInfo;
952 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
953 textureInfo.pNext = nullptr;
954 textureInfo.flags = 0;
955 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
956 textureInfo.pBindings = textureBindings.data();
957
958 vk::DescriptorSetLayout textureLayout;
959 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
960 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
961 }
962
963 VkPipelineLayoutCreateInfo createInfo;
964 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
965 createInfo.pNext = nullptr;
966 createInfo.flags = 0;
967 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
968 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
969 createInfo.pushConstantRangeCount = 0;
970 createInfo.pPushConstantRanges = nullptr;
971
972 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
973
974 return vk::NoError();
975}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500976
977Serial RendererVk::issueProgramSerial()
978{
979 return mProgramSerialFactory.generate();
980}
981
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500982vk::Error RendererVk::getPipeline(const ProgramVk *programVk,
983 const vk::PipelineDesc &desc,
984 vk::PipelineAndSerial **pipelineOut)
985{
986 ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial);
987 ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial);
988
989 // Pull in a compatible RenderPass.
990 vk::RenderPass *compatibleRenderPass = nullptr;
991 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
992
993 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
994 programVk->getLinkedVertexModule(),
995 programVk->getLinkedFragmentModule(), desc, pipelineOut);
996}
997
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400998} // namespace rx