-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.cpp
256 lines (230 loc) · 8.05 KB
/
instance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @file instance_.cpp
* @brief Implements functions for Vulkan instance_ creation and validation.
* @date Created by Renato on 27-12-23.
*/
#include "instance.hpp"
/**
* @brief Checks if the specified extensions and layers are supported.
*
* This function checks whether the required Vulkan extensions and layers are available on the device_.
* It is crucial for ensuring compatibility and stability of Vulkan operations.
*
* @param extensions A vector of Vulkan extension names to check for support.
* @param layers A vector of Vulkan layer names to check for support.
* @param debug Flag indicating whether to enable debug logging.
* @return true if all extensions and layers are supported, false otherwise
*/
bool vkinit::supported(std::vector<const char*>& extensions, std::vector<const char*>& layers, bool debug)
{
//check extension support
std::vector<vk::ExtensionProperties> supportedExtensions = vk::enumerateInstanceExtensionProperties();
if (debug)
{
std::cout << "Device can support the following extensions:\n";
for (vk::ExtensionProperties supportedExtension : supportedExtensions)
{
std::cout << '\t' << supportedExtension.extensionName << '\n';
}
}
bool found;
for (const char* extension : extensions)
{
found = false;
for (vk::ExtensionProperties supportedExtension : supportedExtensions)
{
if (strcmp(extension, supportedExtension.extensionName) == 0)
{
found = true;
if (debug)
{
std::cout << "Extension \"" << extension << "\" is supported!\n";
}
}
}
if (!found)
{
if (debug)
{
std::cout << "Extension \"" << extension << "\" is not supported!\n";
}
return false;
}
}
//check layer support
std::vector<vk::LayerProperties> supportedLayers = vk::enumerateInstanceLayerProperties();
if (debug)
{
std::cout << "Device can support the following layers:\n";
for (vk::LayerProperties supportedLayer : supportedLayers)
{
std::cout << '\t' << supportedLayer.layerName << '\n';
}
}
for (const char* layer : layers)
{
found = false;
for (vk::LayerProperties supportedLayer : supportedLayers)
{
if (strcmp(layer, supportedLayer.layerName) == 0)
{
found = true;
if (debug)
{
std::cout << "Layer \"" << layer << "\" is supported!\n";
}
}
}
if (!found)
{
if (debug)
{
std::cout << "Layer \"" << layer << "\" is not supported!\n";
}
return false;
}
}
return true;
}
/**
* @brief Creates a Vulkan instance_ with specified application information and extensions
*
* This function initializes a Vulkan instance_, which is the first step in working with Vulkan.
* The instance_ is configured with application and engine information, as well as required extensions
* and layers, especially those needed for debugging purposes.
*
* @param debug Flag indicating whether to enable debug logging.
* @param applicationName The name of the application.
* @return A Vulkan instance_, or nullptr if instance_ creation fails.
*/
vk::Instance vkinit::make_instance(bool debug, const char* applicationName)
{
if (debug)
{
std::cout << "Making an instance_...\n";
}
/*
* An instance_ stores all per-application state info, it is a vulkan handle
* (An opaque integer or pointer value used to refer to a Vulkan object)
* side note: in the vulkan.hpp binding it's a wrapper class around a handle
*
* from vulkan_core.h:
* VK_DEFINE_HANDLE(VkInstance)
*
* from vulkan_handles.hpp:
* class Instance {
* ...
* }
*/
/*
* We can scan the system and check which version it will support up to,
* as of vulkan 1.1
*
* VkResult vkEnumerateInstanceVersion(
uint32_t* pApiVersion);
*/
uint32_t version{ 0 };
vkEnumerateInstanceVersion(&version);
if (debug)
{
std::cout << "System can support vulkan Variant: "
<< VK_API_VERSION_VARIANT(version)
<< ", Major: " << VK_API_VERSION_MAJOR(version)
<< ", Minor: " << VK_API_VERSION_MINOR(version)
<< ", Patch: " << VK_API_VERSION_PATCH(version) << '\n';
}
/*
* We can then either use this version
* (We shoud just be sure to set the patch to 0 for best compatibility/stability)
*/
version &= ~(0xFFFU);
/*
* Or drop down to an earlier version to ensure compatibility with more devices
* VK_MAKE_API_VERSION(variant, major, minor, patch)
*/
version = VK_MAKE_API_VERSION(0, 1, 0, 0);
/*
* from vulkan_structs.hpp:
*
* VULKAN_HPP_CONSTEXPR ApplicationInfo( const char * pApplicationName_ = {},
uint32_t applicationVersion_ = {},
const char * pEngineName_ = {},
uint32_t engineVersion_ = {},
uint32_t apiVersion_ = {} )
*/
vk::ApplicationInfo appInfo = vk::ApplicationInfo
(
applicationName,
version,
"Doing it the hard way",
version,
version
);
/*
* Everything with Vulkan is "opt-in", so we need to query which extensions glfw needs
* in order to interface with vulkan.
*/
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
//In order to hook in a custom validation callback
if (debug)
{
extensions.push_back("VK_EXT_debug_utils");
}
if (debug)
{
std::cout << "extensions to be requested:\n";
for (const char* extensionName : extensions)
{
std::cout << "\t\"" << extensionName << "\"\n";
}
}
std::vector<const char*> layers;
if (debug)
{
layers.push_back("VK_LAYER_KHRONOS_validation");
}
if (!supported(extensions, layers, debug))
{
return nullptr;
}
/*
*
* from vulkan_structs.hpp:
*
* InstanceCreateInfo( VULKAN_HPP_NAMESPACE::InstanceCreateFlags flags_ = {},
const VULKAN_HPP_NAMESPACE::ApplicationInfo * pApplicationInfo_ = {},
uint32_t enabledLayerCount_ = {},
const char * const * ppEnabledLayerNames_ = {},
uint32_t enabledExtensionCount_ = {},
const char * const * ppEnabledExtensionNames_ = {} )
*/
vk::InstanceCreateInfo createInfo = vk::InstanceCreateInfo
(
vk::InstanceCreateFlags(),
&appInfo,
static_cast<uint32_t>(layers.size()), layers.data(), // enabled layers
static_cast<uint32_t>(extensions.size()), extensions.data() // enabled extensions
);
try
{
/*
* from vulkan_funcs.h:
*
* createInstance( const VULKAN_HPP_NAMESPACE::InstanceCreateInfo & createInfo,
Optional<const VULKAN_HPP_NAMESPACE::AllocationCallbacks> allocator = nullptr,
Dispatch const & d = ::vk::getDispatchLoaderStatic())
*/
return vk::createInstance(createInfo);
}
catch (vk::SystemError &err)
{
if (debug)
{
std::cout << "Failed to create Instance!\n";
}
return nullptr;
}
}