-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.cpp
212 lines (206 loc) · 6.54 KB
/
device.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
/**
* @file device.cpp
* @brief Defines functions for creating Vulkan devices.
* @date Created by Renato on 27-12-23.
*/
#include "device.hpp"
/**
* @brief Checks if a Vulkan physical device_ supports the required extensions.
*
* @param device The Vulkan physical device_ to check.
* @param requested_extensions A vector of extension names to be checked.
* @param debug Flag indicating whether to enable debug logging.
* @return true if the device_ supports all requested extensions, false otherwise
*/
bool vkinit::CheckDeviceExtensionSupport
(
const vk::PhysicalDevice& device,
const std::vector<const char*>& requested_extensions,
const bool& debug
)
{
std::set<std::string> requiredExtensions(requested_extensions.begin(), requested_extensions.end());
if(debug)
{
std::cout << "Device can support extensions:\n";
}
for(vk::ExtensionProperties& extension : device.enumerateDeviceExtensionProperties())
{
if(debug)
{
std::cout << "\t\""<<extension.extensionName << "\"\n";
}
requiredExtensions.erase(extension.extensionName);
}
return requiredExtensions.empty();
}
/**
* @brief Determines if a Vulkan physical device_ is suitable for the application's needs.
*
* @param device The vulkan physical device_ to evaluate.
* @param debug Flag indicating whether to enable debug logging.
* @return true if the device_ is suitable, false otherwise.
*/
bool vkinit::IsSuitable(const vk::PhysicalDevice& device, const bool debug)
{
if(debug)
{
std::cout << "Checking if device_ is suitable\n";
}
const std::vector<const char*> requestedExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
if(debug)
{
std::cout << "We are requesting device_ extensions:\n";
for(const char* extension : requestedExtensions)
{
std::cout << "\t\"" << extension << "\"\n";
}
}
if(bool extensionSupported = CheckDeviceExtensionSupport(device, requestedExtensions, debug))
{
if(debug && extensionSupported)
{
std::cout << "Device can support the requested extensions!\n";
}
}
else
{
if(debug)
{
std::cout << "Device can't support the requested extensions!\n";
}
return false;
}
return true;
}
/**
* @brief Chooses a suitable Vulkan physical device_ from available devices.
*
* @param instance The Vulkan instance_.
* @param debug Flag indicating whether to enable debug logging.
* @return The chosen Vulkan physical device_.
*/
vk::PhysicalDevice vkinit::ChoosePhysicalDevice(vk::Instance& instance, bool debug)
{
/**
* Choose a suitable physical device_ from a list of candidates.
* Note: Physical device_ are neither created nor destroyed, they exist
* independently to the program.
*/
if(debug)
{
std::cout << "Choosing physical device_...\n";
}
/**
* ResultValueType<std::vector<PhysicalDevice, PhysicalDeviceAllocator>>::type
* Instance::enumeratePhysicalDevices( Dispatch const & d )
* std::vector<vk::PhysicalDevice> instance_.enumeratePhysicalDevices( Dispatch const & d = static/default )
*/
std::vector<vk::PhysicalDevice>availableDevices = instance.enumeratePhysicalDevices();
if(debug)
{
std::cout << "There are " << availableDevices.size() << " physical devices available on this system\n";
}
for(vk::PhysicalDevice device : availableDevices)
{
if(debug)
{
log_device_properties(device);
}
if(IsSuitable(device, debug))
{
return device;
}
}
return nullptr;
}
/**
* @brief Creates a Vulkan logical device_ from a physical device_.
*
* @param physical_device The Vulkan physical device_.
* @param surface The Vulkan surface_.
* @param debug The Vulkan surface_.
* @return The created Vulkan logical device_.
*/
vk::Device vkinit::CreateLogicalDevice(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface, bool debug)
{
vkutil::QueueFamilyIndices indices = vkutil::findQueueFamilies(physical_device, surface, debug);
std::vector<uint32_t>uniqueIndices;
uniqueIndices.push_back(indices.graphicsFamily.value());
if(indices.graphicsFamily.value() != indices.presentFamily.value())
{
uniqueIndices.push_back(indices.presentFamily.value());
}
float queuePriority = 1.0f;
std::vector<vk::DeviceQueueCreateInfo> queueCreateInfo;
for([[maybe_unused]] uint32_t queueFamilyIndex : uniqueIndices)
{
queueCreateInfo.emplace_back
(
vk::DeviceQueueCreateFlags(),
indices.graphicsFamily.value(),
1,
&queuePriority
);
}
std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
vk::PhysicalDeviceFeatures deviceFeatures = vk::PhysicalDeviceFeatures();
std::vector<const char*> enabledLayers;
if(debug)
{
enabledLayers.push_back("VK_LAYER_KHRONOS_validation");
}
vk::DeviceCreateInfo deviceInfo = vk::DeviceCreateInfo
(
vk::DeviceCreateFlags(),
static_cast<uint32_t>(queueCreateInfo.size()),
queueCreateInfo.data(),
static_cast<uint32_t>(enabledLayers.size()),
enabledLayers.data(),
static_cast<uint32_t>(deviceExtensions.size()),
deviceExtensions.data(),
&deviceFeatures
);
try
{
vk::Device device = physical_device.createDevice(deviceInfo);
if(debug)
{
std::cout << "GPU has been successfully abstracted!\n";
}
return device;
}
catch(vk::SystemError &err)
{
if(debug)
{
std::cout << "Device creation failed!\n";
return nullptr;
}
}
return nullptr;
}
/**
* @brief Retrieves graphics and presentation queues from a Vulkan device_.
*
* @param physical_device The Vulkan physical device_.
* @param device The Vulkan logical device_.
* @param surface The Vulkan surface_.
* @param debug Flag indicating whether to enable debug logging.
* @return An array containing the graphics and presentation queues.
*/
std::array<vk::Queue, 2> vkinit::GetQueues(vk::PhysicalDevice physical_device, vk::Device device, vk::SurfaceKHR surface, bool debug)
{
vkutil::QueueFamilyIndices indices = vkutil::findQueueFamilies(physical_device, surface, debug);
return
{
{
device.getQueue(indices.graphicsFamily.value(), 0),
device.getQueue(indices.presentFamily.value(), 0)
}
};
}