blob: 723c0185a8dedd0f36bb78498788b1ef779011ca [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 Madill1f46bc12018-02-20 16:09:43 -050020#include "libANGLE/renderer/vulkan/CommandGraph.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
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070031// Consts
32namespace
33{
34const uint32_t kMockVendorID = 0xba5eba11;
35const uint32_t kMockDeviceID = 0xf005ba11;
36constexpr char kMockDeviceName[] = "Vulkan Mock Device";
37} // anonymous namespace
38
Jamie Madill9e54b5a2016-05-25 12:57:39 -040039namespace rx
40{
41
Jamie Madille09bd5d2016-11-29 16:20:35 -050042namespace
43{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040044// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
45// one for the vertex shader.
46constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050047
48VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
49 const std::vector<const char *> &enabledExtensionNames)
50{
51 // Compile the extensions names into a set.
52 std::set<std::string> extensionNames;
53 for (const auto &extensionProp : extensionProps)
54 {
55 extensionNames.insert(extensionProp.extensionName);
56 }
57
Jamie Madillacf2f3a2017-11-21 19:22:44 -050058 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050059 {
60 if (extensionNames.count(extensionName) == 0)
61 {
62 return VK_ERROR_EXTENSION_NOT_PRESENT;
63 }
64 }
65
66 return VK_SUCCESS;
67}
68
Yuly Novikov199f4292018-01-19 19:04:05 -050069VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
70 VkDebugReportObjectTypeEXT objectType,
71 uint64_t object,
72 size_t location,
73 int32_t messageCode,
74 const char *layerPrefix,
75 const char *message,
76 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050077{
78 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
79 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050080 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050081#if !defined(NDEBUG)
82 // Abort the call in Debug builds.
83 return VK_TRUE;
84#endif
85 }
86 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
87 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050088 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050089 }
90 else
91 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050092 // Uncomment this if you want Vulkan spam.
93 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050094 }
95
96 return VK_FALSE;
97}
98
Yuly Novikov199f4292018-01-19 19:04:05 -050099// If we're loading the validation layers, we could be running from any random directory.
100// Change to the executable directory so we can find the layers, then change back to the
101// previous directory to be safe we don't disrupt the application.
102class ScopedVkLoaderEnvironment : angle::NonCopyable
103{
104 public:
105 ScopedVkLoaderEnvironment(bool enableValidationLayers)
106 : mEnableValidationLayers(enableValidationLayers), mChangedCWD(false)
107 {
108// Changing CWD and setting environment variables makes no sense on Android,
109// since this code is a part of Java application there.
110// Android Vulkan loader doesn't need this either.
111#if !defined(ANGLE_PLATFORM_ANDROID)
112 if (mEnableValidationLayers)
113 {
114 const auto &cwd = angle::GetCWD();
115 if (!cwd.valid())
116 {
117 ERR() << "Error getting CWD for Vulkan layers init.";
118 mEnableValidationLayers = false;
119 }
120 else
121 {
122 mPreviousCWD = cwd.value();
123 const char *exeDir = angle::GetExecutableDirectory();
124 mChangedCWD = angle::SetCWD(exeDir);
125 if (!mChangedCWD)
126 {
127 ERR() << "Error setting CWD for Vulkan layers init.";
128 mEnableValidationLayers = false;
129 }
130 }
131 }
132
133 // Override environment variable to use the ANGLE layers.
134 if (mEnableValidationLayers)
135 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700136 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500137 {
138 ERR() << "Error setting environment for Vulkan layers init.";
139 mEnableValidationLayers = false;
140 }
141 }
142#endif // !defined(ANGLE_PLATFORM_ANDROID)
143 }
144
145 ~ScopedVkLoaderEnvironment()
146 {
147 if (mChangedCWD)
148 {
149#if !defined(ANGLE_PLATFORM_ANDROID)
150 ASSERT(mPreviousCWD.valid());
151 angle::SetCWD(mPreviousCWD.value().c_str());
152#endif // !defined(ANGLE_PLATFORM_ANDROID)
153 }
154 }
155
156 bool canEnableValidationLayers() { return mEnableValidationLayers; }
157
158 private:
159 bool mEnableValidationLayers;
160 bool mChangedCWD;
161 Optional<std::string> mPreviousCWD;
162};
163
Jamie Madille09bd5d2016-11-29 16:20:35 -0500164} // anonymous namespace
165
Jamie Madill49ac74b2017-12-21 14:42:33 -0500166// CommandBatch implementation.
167RendererVk::CommandBatch::CommandBatch()
168{
169}
170
171RendererVk::CommandBatch::~CommandBatch()
172{
173}
174
175RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
176 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
177{
178}
179
180RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
181{
182 std::swap(commandPool, other.commandPool);
183 std::swap(fence, other.fence);
184 std::swap(serial, other.serial);
185 return *this;
186}
187
Jamie Madill9f2a8612017-11-30 12:43:09 -0500188// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500189RendererVk::RendererVk()
190 : mCapsInitialized(false),
191 mInstance(VK_NULL_HANDLE),
192 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500193 mDebugReportCallback(VK_NULL_HANDLE),
194 mPhysicalDevice(VK_NULL_HANDLE),
195 mQueue(VK_NULL_HANDLE),
196 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
197 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500198 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400199 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
200 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill49ac74b2017-12-21 14:42:33 -0500201 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400202{
203}
204
205RendererVk::~RendererVk()
206{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500207 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500208 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500209 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
210 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500211 if (error.isError())
212 {
213 ERR() << "Error during VK shutdown: " << error;
214 }
215 }
216
Jamie Madill8c3988c2017-12-21 14:44:56 -0500217 for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts)
218 {
219 descriptorSetLayout.destroy(mDevice);
220 }
221
222 mGraphicsPipelineLayout.destroy(mDevice);
223
Jamie Madill9f2a8612017-11-30 12:43:09 -0500224 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500225 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400226 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500227
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500228 if (mGlslangWrapper)
229 {
230 GlslangWrapper::ReleaseReference();
231 mGlslangWrapper = nullptr;
232 }
233
Jamie Madill5deea722017-02-16 10:44:46 -0500234 if (mCommandPool.valid())
235 {
236 mCommandPool.destroy(mDevice);
237 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500238
239 if (mDevice)
240 {
241 vkDestroyDevice(mDevice, nullptr);
242 mDevice = VK_NULL_HANDLE;
243 }
244
Jamie Madill0448ec82016-12-23 13:41:47 -0500245 if (mDebugReportCallback)
246 {
247 ASSERT(mInstance);
248 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
249 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
250 ASSERT(destroyDebugReportCallback);
251 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
252 }
253
Jamie Madill4d0bf552016-12-28 15:45:24 -0500254 if (mInstance)
255 {
256 vkDestroyInstance(mInstance, nullptr);
257 mInstance = VK_NULL_HANDLE;
258 }
259
260 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500261}
262
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700263void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
264 bool preferMockICD,
265 VkPhysicalDevice *physicalDeviceOut,
266 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
267{
268 ASSERT(!physicalDevices.empty());
269 if (preferMockICD)
270 {
271 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
272 {
273 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
274 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
275 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
276 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
277 {
278 *physicalDeviceOut = physicalDevice;
279 return;
280 }
281 }
282 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
283 "physicalDevice instead.";
284 }
285
286 // Fall back to first device.
287 *physicalDeviceOut = physicalDevices[0];
288 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
289}
290
Frank Henigman29f148b2016-11-23 21:05:36 -0500291vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500292{
Jamie Madilldf9ad2b2018-02-02 12:40:01 -0500293 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500294 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Jamie Madilla66779f2017-01-06 10:43:44 -0500295
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700296 bool enableNullDriver = false;
297#if !defined(ANGLE_PLATFORM_ANDROID)
298 // Mock ICD does not currently run on Android
299 enableNullDriver = (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
300 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
301 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
302 if (enableNullDriver)
303 {
304 // Override environment variable to use built Mock ICD
305 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
306 ANGLE_VK_CHECK(angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON),
307 VK_ERROR_INITIALIZATION_FAILED);
308 }
309#endif // !defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill0448ec82016-12-23 13:41:47 -0500310 // Gather global layer properties.
311 uint32_t instanceLayerCount = 0;
312 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
313
314 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
315 if (instanceLayerCount > 0)
316 {
317 ANGLE_VK_TRY(
318 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
319 }
320
Jamie Madille09bd5d2016-11-29 16:20:35 -0500321 uint32_t instanceExtensionCount = 0;
322 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
323
324 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
325 if (instanceExtensionCount > 0)
326 {
327 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
328 instanceExtensionProps.data()));
329 }
330
Yuly Novikov199f4292018-01-19 19:04:05 -0500331 const char *const *enabledLayerNames = nullptr;
332 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500333 if (mEnableValidationLayers)
334 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500335 bool layersRequested =
336 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
337 mEnableValidationLayers = GetAvailableValidationLayers(
338 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500339 }
340
Jamie Madille09bd5d2016-11-29 16:20:35 -0500341 std::vector<const char *> enabledInstanceExtensions;
342 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500343 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500344
Jamie Madill0448ec82016-12-23 13:41:47 -0500345 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
346 if (mEnableValidationLayers)
347 {
348 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
349 }
350
Jamie Madille09bd5d2016-11-29 16:20:35 -0500351 // Verify the required extensions are in the extension names set. Fail if not.
352 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
353
Jamie Madill327ba852016-11-30 12:38:28 -0500354 VkApplicationInfo applicationInfo;
355 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
356 applicationInfo.pNext = nullptr;
357 applicationInfo.pApplicationName = "ANGLE";
358 applicationInfo.applicationVersion = 1;
359 applicationInfo.pEngineName = "ANGLE";
360 applicationInfo.engineVersion = 1;
361 applicationInfo.apiVersion = VK_API_VERSION_1_0;
362
363 VkInstanceCreateInfo instanceInfo;
364 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
365 instanceInfo.pNext = nullptr;
366 instanceInfo.flags = 0;
367 instanceInfo.pApplicationInfo = &applicationInfo;
368
Jamie Madille09bd5d2016-11-29 16:20:35 -0500369 // Enable requested layers and extensions.
370 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
371 instanceInfo.ppEnabledExtensionNames =
372 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500373 instanceInfo.enabledLayerCount = enabledLayerCount;
374 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500375
376 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
377
Jamie Madill0448ec82016-12-23 13:41:47 -0500378 if (mEnableValidationLayers)
379 {
380 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
381
382 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
383 debugReportInfo.pNext = nullptr;
384 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
385 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
386 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
387 debugReportInfo.pfnCallback = &DebugReportCallback;
388 debugReportInfo.pUserData = this;
389
390 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
391 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
392 ASSERT(createDebugReportCallback);
393 ANGLE_VK_TRY(
394 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
395 }
396
Jamie Madill4d0bf552016-12-28 15:45:24 -0500397 uint32_t physicalDeviceCount = 0;
398 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
399 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
400
401 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700402 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
403 ANGLE_VK_TRY(
404 vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, physicalDevices.data()));
405 ChoosePhysicalDevice(physicalDevices, enableNullDriver, &mPhysicalDevice,
406 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500407
408 // Ensure we can find a graphics queue family.
409 uint32_t queueCount = 0;
410 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
411
412 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
413
414 mQueueFamilyProperties.resize(queueCount);
415 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
416 mQueueFamilyProperties.data());
417
418 size_t graphicsQueueFamilyCount = false;
419 uint32_t firstGraphicsQueueFamily = 0;
420 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
421 {
422 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
423 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
424 {
425 ASSERT(queueInfo.queueCount > 0);
426 graphicsQueueFamilyCount++;
427 if (firstGraphicsQueueFamily == 0)
428 {
429 firstGraphicsQueueFamily = familyIndex;
430 }
431 break;
432 }
433 }
434
435 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
436
437 // If only one queue family, go ahead and initialize the device. If there is more than one
438 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
439 if (graphicsQueueFamilyCount == 1)
440 {
441 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
442 }
443
Jamie Madill035fd6b2017-10-03 15:43:22 -0400444 // Store the physical device memory properties so we can find the right memory pools.
445 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500446
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500447 mGlslangWrapper = GlslangWrapper::GetReference();
448
Jamie Madill6a89d222017-11-02 11:59:51 -0400449 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500450 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
451 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400452
Jamie Madill8c3988c2017-12-21 14:44:56 -0500453 // Initialize the pipeline layout for GL programs.
454 ANGLE_TRY(initGraphicsPipelineLayout());
455
Jamie Madill327ba852016-11-30 12:38:28 -0500456 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400457}
458
Jamie Madill4d0bf552016-12-28 15:45:24 -0500459vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
460{
461 uint32_t deviceLayerCount = 0;
462 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
463
464 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
465 if (deviceLayerCount > 0)
466 {
467 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
468 deviceLayerProps.data()));
469 }
470
471 uint32_t deviceExtensionCount = 0;
472 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
473 &deviceExtensionCount, nullptr));
474
475 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
476 if (deviceExtensionCount > 0)
477 {
478 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
479 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
480 }
481
Yuly Novikov199f4292018-01-19 19:04:05 -0500482 const char *const *enabledLayerNames = nullptr;
483 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500484 if (mEnableValidationLayers)
485 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500486 mEnableValidationLayers = GetAvailableValidationLayers(
487 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500488 }
489
490 std::vector<const char *> enabledDeviceExtensions;
491 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
492
493 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
494
495 VkDeviceQueueCreateInfo queueCreateInfo;
496
497 float zeroPriority = 0.0f;
498
499 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
500 queueCreateInfo.pNext = nullptr;
501 queueCreateInfo.flags = 0;
502 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
503 queueCreateInfo.queueCount = 1;
504 queueCreateInfo.pQueuePriorities = &zeroPriority;
505
506 // Initialize the device
507 VkDeviceCreateInfo createInfo;
508
509 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
510 createInfo.pNext = nullptr;
511 createInfo.flags = 0;
512 createInfo.queueCreateInfoCount = 1;
513 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500514 createInfo.enabledLayerCount = enabledLayerCount;
515 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500516 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
517 createInfo.ppEnabledExtensionNames =
518 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
519 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
520
521 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
522
523 mCurrentQueueFamilyIndex = queueFamilyIndex;
524
525 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
526
527 // Initialize the command pool now that we know the queue family index.
528 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500529 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
530 commandPoolInfo.pNext = nullptr;
531 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500532 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
533
Jamie Madill5deea722017-02-16 10:44:46 -0500534 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500535
Jamie Madill4d0bf552016-12-28 15:45:24 -0500536 return vk::NoError();
537}
538
539vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
540{
541 // We've already initialized a device, and can't re-create it unless it's never been used.
542 // TODO(jmadill): Handle the re-creation case if necessary.
543 if (mDevice != VK_NULL_HANDLE)
544 {
545 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
546
547 // Check if the current device supports present on this surface.
548 VkBool32 supportsPresent = VK_FALSE;
549 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
550 surface, &supportsPresent));
551
552 return (supportsPresent == VK_TRUE);
553 }
554
555 // Find a graphics and present queue.
556 Optional<uint32_t> newPresentQueue;
557 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
558 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
559 {
560 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
561 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
562 {
563 VkBool32 supportsPresent = VK_FALSE;
564 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
565 &supportsPresent));
566
567 if (supportsPresent == VK_TRUE)
568 {
569 newPresentQueue = queueIndex;
570 break;
571 }
572 }
573 }
574
575 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
576 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
577
578 return newPresentQueue.value();
579}
580
581std::string RendererVk::getVendorString() const
582{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300583 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500584}
585
Jamie Madille09bd5d2016-11-29 16:20:35 -0500586std::string RendererVk::getRendererDescription() const
587{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500588 std::stringstream strstr;
589
590 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
591
592 strstr << "Vulkan ";
593 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
594 strstr << VK_VERSION_MINOR(apiVersion) << ".";
595 strstr << VK_VERSION_PATCH(apiVersion);
596
Olli Etuahoc6a06182018-04-13 14:11:46 +0300597 strstr << "(";
598
599 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
600 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
601 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
602 // driver detection.
603 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
604 {
605 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
606 }
607
608 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609
610 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500611}
612
Jamie Madillacccc6c2016-05-03 17:22:10 -0400613void RendererVk::ensureCapsInitialized() const
614{
615 if (!mCapsInitialized)
616 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500617 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Luc Ferrone4741fd2018-01-25 13:25:27 -0500618 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400619 mCapsInitialized = true;
620 }
621}
622
Jamie Madillacccc6c2016-05-03 17:22:10 -0400623const gl::Caps &RendererVk::getNativeCaps() const
624{
625 ensureCapsInitialized();
626 return mNativeCaps;
627}
628
629const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
630{
631 ensureCapsInitialized();
632 return mNativeTextureCaps;
633}
634
635const gl::Extensions &RendererVk::getNativeExtensions() const
636{
637 ensureCapsInitialized();
638 return mNativeExtensions;
639}
640
641const gl::Limitations &RendererVk::getNativeLimitations() const
642{
643 ensureCapsInitialized();
644 return mNativeLimitations;
645}
646
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400647uint32_t RendererVk::getMaxActiveTextures()
648{
649 // TODO(lucferron): expose this limitation to GL in Context Caps
650 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
651 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
652}
653
654uint32_t RendererVk::getUniformBufferDescriptorCount()
655{
656 return kUniformBufferDescriptorsPerDescriptorSet;
657}
658
Jamie Madill49ac74b2017-12-21 14:42:33 -0500659const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500660{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500661 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500662}
663
Jamie Madill49ac74b2017-12-21 14:42:33 -0500664vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500665{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500666 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500667 {
668 vk::CommandBuffer commandBatch;
669 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400670
Jamie Madill49ac74b2017-12-21 14:42:33 -0500671 VkSubmitInfo submitInfo;
672 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
673 submitInfo.pNext = nullptr;
674 submitInfo.waitSemaphoreCount = 0;
675 submitInfo.pWaitSemaphores = nullptr;
676 submitInfo.pWaitDstStageMask = nullptr;
677 submitInfo.commandBufferCount = 1;
678 submitInfo.pCommandBuffers = commandBatch.ptr();
679 submitInfo.signalSemaphoreCount = 0;
680 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500681
Jamie Madill49ac74b2017-12-21 14:42:33 -0500682 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
683 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500684
Jamie Madill4c26fc22017-02-24 11:04:10 -0500685 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500686 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400687 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500688 return vk::NoError();
689}
690
Jamie Madill0c0dc342017-03-24 14:18:51 -0400691void RendererVk::freeAllInFlightResources()
692{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500693 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400694 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500695 batch.fence.destroy(mDevice);
696 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400697 }
698 mInFlightCommands.clear();
699
700 for (auto &garbage : mGarbage)
701 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400702 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400703 }
704 mGarbage.clear();
705}
706
Jamie Madill4c26fc22017-02-24 11:04:10 -0500707vk::Error RendererVk::checkInFlightCommands()
708{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500709 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500710
Jamie Madill49ac74b2017-12-21 14:42:33 -0500711 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500712 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500713 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400714 if (result == VK_NOT_READY)
715 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500716
Jamie Madill0c0dc342017-03-24 14:18:51 -0400717 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500718 ASSERT(batch.serial > mLastCompletedQueueSerial);
719 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400720
Jamie Madill49ac74b2017-12-21 14:42:33 -0500721 batch.fence.destroy(mDevice);
722 batch.commandPool.destroy(mDevice);
723 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500724 }
725
Jamie Madill49ac74b2017-12-21 14:42:33 -0500726 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400727
728 size_t freeIndex = 0;
729 for (; freeIndex < mGarbage.size(); ++freeIndex)
730 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500731 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400732 break;
733 }
734
735 // Remove the entries from the garbage list - they should be ready to go.
736 if (freeIndex > 0)
737 {
738 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500739 }
740
Jamie Madill4c26fc22017-02-24 11:04:10 -0500741 return vk::NoError();
742}
743
Jamie Madill49ac74b2017-12-21 14:42:33 -0500744vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500745{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500746 VkFenceCreateInfo fenceInfo;
747 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
748 fenceInfo.pNext = nullptr;
749 fenceInfo.flags = 0;
750
751 CommandBatch batch;
752 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
753
754 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500755
756 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500757 batch.commandPool = std::move(mCommandPool);
758 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500759
Jamie Madill49ac74b2017-12-21 14:42:33 -0500760 mInFlightCommands.emplace_back(std::move(batch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400761
762 // Sanity check.
763 ASSERT(mInFlightCommands.size() < 1000u);
764
765 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400766 // TODO(jmadill): Overflow check.
767 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400768
769 ANGLE_TRY(checkInFlightCommands());
770
Jamie Madill49ac74b2017-12-21 14:42:33 -0500771 // Simply null out the command buffer here - it was allocated using the command pool.
772 commandBuffer.releaseHandle();
773
774 // Reallocate the command pool for next frame.
775 // TODO(jmadill): Consider reusing command pools.
776 VkCommandPoolCreateInfo poolInfo;
777 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
778 poolInfo.pNext = nullptr;
779 poolInfo.flags = 0;
780 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
781
782 mCommandPool.init(mDevice, poolInfo);
783
Jamie Madill4c26fc22017-02-24 11:04:10 -0500784 return vk::NoError();
785}
786
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500787GlslangWrapper *RendererVk::getGlslangWrapper()
788{
789 return mGlslangWrapper;
790}
791
Jamie Madill4c26fc22017-02-24 11:04:10 -0500792Serial RendererVk::getCurrentQueueSerial() const
793{
794 return mCurrentQueueSerial;
795}
796
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400797bool RendererVk::isResourceInUse(const vk::CommandGraphResource &resource)
Jamie Madill97760352017-11-09 13:08:29 -0500798{
799 return isSerialInUse(resource.getQueueSerial());
800}
801
802bool RendererVk::isSerialInUse(Serial serial)
803{
804 return serial > mLastCompletedQueueSerial;
805}
806
Jamie Madill9f2a8612017-11-30 12:43:09 -0500807vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
808 vk::RenderPass **renderPassOut)
809{
810 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
811 renderPassOut);
812}
813
Jamie Madillbef918c2017-12-13 13:11:30 -0500814vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
815 const vk::AttachmentOpsArray &ops,
816 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500817{
Jamie Madillbef918c2017-12-13 13:11:30 -0500818 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
819 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500820}
821
Jamie Madill1f46bc12018-02-20 16:09:43 -0500822vk::CommandGraphNode *RendererVk::allocateCommandNode()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500823{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500824 return mCommandGraph.allocateNode();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500825}
826
827vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
828{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500829 return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache,
830 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500831}
832
833vk::Error RendererVk::flush(const gl::Context *context,
834 const vk::Semaphore &waitSemaphore,
835 const vk::Semaphore &signalSemaphore)
836{
837 vk::CommandBuffer commandBatch;
838 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
839
840 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
841
842 VkSubmitInfo submitInfo;
843 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
844 submitInfo.pNext = nullptr;
845 submitInfo.waitSemaphoreCount = 1;
846 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
847 submitInfo.pWaitDstStageMask = &waitStageMask;
848 submitInfo.commandBufferCount = 1;
849 submitInfo.pCommandBuffers = commandBatch.ptr();
850 submitInfo.signalSemaphoreCount = 1;
851 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
852
853 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
854 return vk::NoError();
855}
856
Jamie Madill8c3988c2017-12-21 14:44:56 -0500857const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const
858{
859 return mGraphicsPipelineLayout;
860}
861
862const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const
863{
864 return mGraphicsDescriptorSetLayouts;
865}
866
867vk::Error RendererVk::initGraphicsPipelineLayout()
868{
869 ASSERT(!mGraphicsPipelineLayout.valid());
870
871 // Create two descriptor set layouts: one for default uniform info, and one for textures.
872 // Skip one or both if there are no uniforms.
873 VkDescriptorSetLayoutBinding uniformBindings[2];
874 uint32_t blockCount = 0;
875
876 {
877 auto &layoutBinding = uniformBindings[blockCount];
878
879 layoutBinding.binding = blockCount;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400880 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500881 layoutBinding.descriptorCount = 1;
882 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
883 layoutBinding.pImmutableSamplers = nullptr;
884
885 blockCount++;
886 }
887
888 {
889 auto &layoutBinding = uniformBindings[blockCount];
890
891 layoutBinding.binding = blockCount;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400892 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500893 layoutBinding.descriptorCount = 1;
894 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
895 layoutBinding.pImmutableSamplers = nullptr;
896
897 blockCount++;
898 }
899
900 {
901 VkDescriptorSetLayoutCreateInfo uniformInfo;
902 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
903 uniformInfo.pNext = nullptr;
904 uniformInfo.flags = 0;
905 uniformInfo.bindingCount = blockCount;
906 uniformInfo.pBindings = uniformBindings;
907
908 vk::DescriptorSetLayout uniformLayout;
909 ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo));
910 mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout));
911 }
912
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400913 std::vector<VkDescriptorSetLayoutBinding> textureBindings(getMaxActiveTextures());
Jamie Madill8c3988c2017-12-21 14:44:56 -0500914
915 // TODO(jmadill): This approach might not work well for texture arrays.
Yuly Novikov37968132018-01-23 18:19:29 -0500916 for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex)
Jamie Madill8c3988c2017-12-21 14:44:56 -0500917 {
918 VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex];
919
920 layoutBinding.binding = textureIndex;
921 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
922 layoutBinding.descriptorCount = 1;
923 layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
924 layoutBinding.pImmutableSamplers = nullptr;
925 }
926
927 {
928 VkDescriptorSetLayoutCreateInfo textureInfo;
929 textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
930 textureInfo.pNext = nullptr;
931 textureInfo.flags = 0;
932 textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size());
933 textureInfo.pBindings = textureBindings.data();
934
935 vk::DescriptorSetLayout textureLayout;
936 ANGLE_TRY(textureLayout.init(mDevice, textureInfo));
937 mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout));
938 }
939
940 VkPipelineLayoutCreateInfo createInfo;
941 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
942 createInfo.pNext = nullptr;
943 createInfo.flags = 0;
944 createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size());
945 createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr();
946 createInfo.pushConstantRangeCount = 0;
947 createInfo.pPushConstantRanges = nullptr;
948
949 ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo));
950
951 return vk::NoError();
952}
Jamie Madillf2f6d372018-01-10 21:37:23 -0500953
954Serial RendererVk::issueProgramSerial()
955{
956 return mProgramSerialFactory.generate();
957}
958
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500959vk::Error RendererVk::getPipeline(const ProgramVk *programVk,
960 const vk::PipelineDesc &desc,
Luc Ferronceb71902018-02-05 15:18:47 -0500961 const gl::AttributesMask &activeAttribLocationsMask,
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500962 vk::PipelineAndSerial **pipelineOut)
963{
964 ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial);
965 ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial);
966
967 // Pull in a compatible RenderPass.
968 vk::RenderPass *compatibleRenderPass = nullptr;
969 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
970
971 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500972 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500973 programVk->getLinkedFragmentModule(), desc, pipelineOut);
974}
975
Jamie Madilld47044a2018-04-27 11:45:03 -0400976vk::ShaderLibrary *RendererVk::getShaderLibrary()
977{
978 return &mShaderLibrary;
979}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400980} // namespace rx