blob: 14c3c14c22e3747ce1f8adb5d1df59bcd79a277b [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 Madille09bd5d2016-11-29 16:20:35 -050012#include <EGL/eglext.h>
13
Jamie Madill9e54b5a2016-05-25 12:57:39 -040014#include "common/debug.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050015#include "libANGLE/renderer/vulkan/CompilerVk.h"
16#include "libANGLE/renderer/vulkan/FramebufferVk.h"
17#include "libANGLE/renderer/vulkan/TextureVk.h"
18#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
19#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040020
21namespace rx
22{
23
Jamie Madille09bd5d2016-11-29 16:20:35 -050024namespace
25{
26
27VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
28 const std::vector<const char *> &enabledExtensionNames)
29{
30 // Compile the extensions names into a set.
31 std::set<std::string> extensionNames;
32 for (const auto &extensionProp : extensionProps)
33 {
34 extensionNames.insert(extensionProp.extensionName);
35 }
36
37 for (const auto &extensionName : enabledExtensionNames)
38 {
39 if (extensionNames.count(extensionName) == 0)
40 {
41 return VK_ERROR_EXTENSION_NOT_PRESENT;
42 }
43 }
44
45 return VK_SUCCESS;
46}
47
48} // anonymous namespace
49
50RendererVk::RendererVk() : mCapsInitialized(false), mInstance(VK_NULL_HANDLE)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040051{
52}
53
54RendererVk::~RendererVk()
55{
Jamie Madill327ba852016-11-30 12:38:28 -050056 vkDestroyInstance(mInstance, nullptr);
57}
58
Jamie Madille09bd5d2016-11-29 16:20:35 -050059vk::Error RendererVk::initialize(const egl::AttributeMap &attribs)
Jamie Madill327ba852016-11-30 12:38:28 -050060{
Jamie Madille09bd5d2016-11-29 16:20:35 -050061 uint32_t instanceExtensionCount = 0;
62 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
63
64 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
65 if (instanceExtensionCount > 0)
66 {
67 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
68 instanceExtensionProps.data()));
69 }
70
71 std::vector<const char *> enabledInstanceExtensions;
72 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
73#if defined(ANGLE_PLATFORM_WINDOWS)
74 enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
75#else
76#error Unsupported Vulkan platform.
77#endif // defined(ANGLE_PLATFORM_WINDOWS)
78
79 // Verify the required extensions are in the extension names set. Fail if not.
80 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
81
Jamie Madill327ba852016-11-30 12:38:28 -050082 VkApplicationInfo applicationInfo;
83 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
84 applicationInfo.pNext = nullptr;
85 applicationInfo.pApplicationName = "ANGLE";
86 applicationInfo.applicationVersion = 1;
87 applicationInfo.pEngineName = "ANGLE";
88 applicationInfo.engineVersion = 1;
89 applicationInfo.apiVersion = VK_API_VERSION_1_0;
90
91 VkInstanceCreateInfo instanceInfo;
92 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
93 instanceInfo.pNext = nullptr;
94 instanceInfo.flags = 0;
95 instanceInfo.pApplicationInfo = &applicationInfo;
96
Jamie Madille09bd5d2016-11-29 16:20:35 -050097 // Enable requested layers and extensions.
98 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
99 instanceInfo.ppEnabledExtensionNames =
100 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
101 instanceInfo.enabledLayerCount = 0u;
102 instanceInfo.ppEnabledLayerNames = nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500103
104 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
105
106 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400107}
108
Jamie Madille09bd5d2016-11-29 16:20:35 -0500109std::string RendererVk::getRendererDescription() const
110{
111 // TODO(jmadill): Description.
112 return "Vulkan";
113}
114
Jamie Madillacccc6c2016-05-03 17:22:10 -0400115void RendererVk::ensureCapsInitialized() const
116{
117 if (!mCapsInitialized)
118 {
119 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
120 mCapsInitialized = true;
121 }
122}
123
124void RendererVk::generateCaps(gl::Caps * /*outCaps*/,
125 gl::TextureCapsMap * /*outTextureCaps*/,
126 gl::Extensions * /*outExtensions*/,
127 gl::Limitations * /* outLimitations */) const
128{
Jamie Madill327ba852016-11-30 12:38:28 -0500129 // TODO(jmadill): Caps.
Jamie Madillacccc6c2016-05-03 17:22:10 -0400130}
131
132const gl::Caps &RendererVk::getNativeCaps() const
133{
134 ensureCapsInitialized();
135 return mNativeCaps;
136}
137
138const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
139{
140 ensureCapsInitialized();
141 return mNativeTextureCaps;
142}
143
144const gl::Extensions &RendererVk::getNativeExtensions() const
145{
146 ensureCapsInitialized();
147 return mNativeExtensions;
148}
149
150const gl::Limitations &RendererVk::getNativeLimitations() const
151{
152 ensureCapsInitialized();
153 return mNativeLimitations;
154}
155
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400156} // namespace rx