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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN
#include <cstring>
#include <cstdio>
#include <vector>
#include <cassert>
#include "window.hpp"
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
GLFWwindow *window = nullptr;
VkInstance vkInstance = nullptr;
VkPhysicalDevice vkPhysicalDevice = nullptr;
VkDevice vkDevice = nullptr;
VkSurfaceKHR vkSurfaceKHR = nullptr;
VkDebugReportCallbackEXT vkDebugReport = nullptr;
VkAllocationCallbacks vkAllocatorStruct = {};
VkAllocationCallbacks *vkAllocator = nullptr;
const bool ENABLE_VALIDATION_LAYERS = true;
const bool VULKAN_DEBUG_REPORT = true;
const std::vector<const char *> REQUIRED_EXTENSIONS = std::vector<const char *> {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkBool32 UserDebugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) {
printf("Validation Layer: %s\n", pCallbackData->pMessage);
return VK_FALSE;
}
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugReport(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, unsigned long object, size_t location, int messageCode, const char *pLayerPrefix, const char *pMessage, void *pUserData) {
(void)flags;
(void)object;
(void)location;
(void)messageCode;
(void)pUserData;
(void)pLayerPrefix; // Unused arguments
fprintf(stderr, "[vulkan] Debug report from ObjectType: %i\nMessage: %s\n\n", objectType, pMessage);
return VK_FALSE;
}
unsigned int FindQueueFamilyIndex(VkPhysicalDevice device, short hasPresentSupport = -1, VkQueueFlagBits includeBits = (VkQueueFlagBits)0U, VkQueueFlagBits excludeBits = (VkQueueFlagBits)0U) {
if (hasPresentSupport == -1 && includeBits == 0 && excludeBits == 0) {
return 0U;
}
unsigned int queueFamilyPropertyCount = 0U;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyPropertyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queueFamilyPropertyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyPropertyCount, queueFamilyProperties.data());
for (unsigned int i = 0; i < queueFamilyProperties.size(); i++) {
if (includeBits != 0 && (queueFamilyProperties[i].queueFlags & includeBits) == 0) {
continue;
}
if (excludeBits != 0 && (queueFamilyProperties[i].queueFlags & excludeBits) != 0) {
continue;
}
if (hasPresentSupport != -1) {
VkBool32 presentSupport;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, vkSurfaceKHR, &presentSupport);
if (presentSupport != hasPresentSupport) {
continue;
}
}
return i;
}
return 0xFFFFFFFF;
}
void InitVulkan() {
if (ENABLE_VALIDATION_LAYERS) {
unsigned int layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
printf("Available Layers:\n");
for (const auto &layerProperties : availableLayers) {
printf("\t%s\n", layerProperties.layerName);
}
}
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.apiVersion = VK_API_VERSION_1_3;
appInfo.pApplicationName = "pikul";
appInfo.pEngineName = "pikul";
appInfo.applicationVersion = 1;
appInfo.engineVersion = 1;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledLayerCount = 0;
std::vector<const char *> enabledLayerNames(1);
enabledLayerNames[0] = "VK_LAYER_KHRONOS_validation";
printf("Requested Layers:\n");
for (const char *s : enabledLayerNames) {
printf("\t%s\n", s);
}
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{};
debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
if (ENABLE_VALIDATION_LAYERS) {
createInfo.enabledLayerCount = enabledLayerNames.size();
createInfo.ppEnabledLayerNames = enabledLayerNames.data();
debugCreateInfo.messageSeverity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debugCreateInfo.messageType =
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
debugCreateInfo.pfnUserCallback = UserDebugCallback;
debugCreateInfo.pUserData = nullptr;
debugCreateInfo.pNext = nullptr;
createInfo.pNext = &debugCreateInfo;
}
std::vector<const char *> allGlfwExtensions;
{
unsigned int extensionCount = 0;
const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&extensionCount);
allGlfwExtensions.reserve(extensionCount + 2);
copy(&glfwExtensions[0], &glfwExtensions[extensionCount], back_inserter(allGlfwExtensions));
if (ENABLE_VALIDATION_LAYERS) {
allGlfwExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
allGlfwExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}
createInfo.ppEnabledExtensionNames = allGlfwExtensions.data();
createInfo.enabledExtensionCount = allGlfwExtensions.size();
}
printf("Required Extensions:\n");
for (const auto &ext : allGlfwExtensions) {
printf("\t%s\n", ext);
}
if (ENABLE_VALIDATION_LAYERS) {
unsigned int extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
printf("Available Extensions:\n");
for (const auto &extension : extensions) {
printf("\t%s\n", extension.extensionName);
}
}
auto result = vkCreateInstance(&createInfo, nullptr, &vkInstance);
if (result != VK_SUCCESS) {
printf("Failed to create VkInstance! : %d\n", result);
throw result;
}
if (VULKAN_DEBUG_REPORT) {
PFN_vkCreateDebugReportCallbackEXT func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkCreateDebugReportCallbackEXT");
assert(func != nullptr && "vkCreateDebugReportCallbackEXT ProcAddr was nullptr");
VkDebugReportCallbackCreateInfoEXT debugReportCallbackCreateInfo{};
debugReportCallbackCreateInfo.flags =
VK_DEBUG_REPORT_ERROR_BIT_EXT |
VK_DEBUG_REPORT_WARNING_BIT_EXT |
VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
debugReportCallbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
debugReportCallbackCreateInfo.pfnCallback = DebugReport;
debugReportCallbackCreateInfo.pUserData = nullptr;
VkResult result = func(vkInstance, &debugReportCallbackCreateInfo, nullptr, &vkDebugReport);
if (result != VK_SUCCESS) {
fprintf(stderr, "%s\n", "Failed to create debug report!");
}
}
// create surface
if (glfwCreateWindowSurface(vkInstance, window, nullptr, &vkSurfaceKHR) != VK_SUCCESS) {
throw "Failed to create window surface";
}
// pick physical device
unsigned int physicalDeviceCount = 0;
vkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, nullptr);
assert(physicalDeviceCount > 0);
std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
vkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices.data());
unsigned int graphicsFamilyIndex = 0;
unsigned int presentFamilyIndex = 0;
for (const auto &device : physicalDevices)
{
// check queue families
graphicsFamilyIndex = FindQueueFamilyIndex(device, -1, VK_QUEUE_GRAPHICS_BIT);
presentFamilyIndex = FindQueueFamilyIndex(device, 1);
if (graphicsFamilyIndex == 0xFFFFFFFF || presentFamilyIndex == 0xFFFFFFFF) {
continue;
}
// check device extension support
std::vector<const char *> requiredExtensions(REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
unsigned int extensionCount = 0;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> extensionProperties(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, extensionProperties.data());
for (const auto &property : extensionProperties) {
auto it = requiredExtensions.begin();
while (it != requiredExtensions.end()) {
if (strcmp(*it.base(), property.extensionName)) {
requiredExtensions.erase(it);
} else {
it++;
}
}
}
if (requiredExtensions.empty() == false) {
continue;
}
// surface formats
unsigned int surfaceCount = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, vkSurfaceKHR, &surfaceCount, nullptr);
if (surfaceCount == 0) {
continue;
}
// check present modes
unsigned int presentModeCount = 0;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, vkSurfaceKHR, &presentModeCount, nullptr);
if (presentModeCount == 0) {
continue;
}
vkPhysicalDevice = device;
break;
}
assert(vkPhysicalDevice != nullptr && "Failed to find suitable physical device");
// Create logical device
{
unsigned int transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, 0, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT);
if (transferQueueIndex == 0xFFFFFFFF) {
transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT);
}
if (transferQueueIndex == 0xFFFFFFFF) {
transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT);
}
if (transferQueueIndex == 0xFFFFFFFF) {
printf("Failed to find transfer queue index");
throw("Failed to find transfer queue index");
}
VkDeviceQueueCreateInfo deviceQueueCreateInfos[2];
float graphicsPriorities[1] = { 1.0f };
deviceQueueCreateInfos[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfos[0].pNext = nullptr;
deviceQueueCreateInfos[0].flags = 0;
deviceQueueCreateInfos[0].queueFamilyIndex = graphicsFamilyIndex;
deviceQueueCreateInfos[0].queueCount = 1l;
deviceQueueCreateInfos[0].pQueuePriorities = graphicsPriorities;
float transferPriorities[1] = { 0.5f };
deviceQueueCreateInfos[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfos[1].pNext = nullptr;
deviceQueueCreateInfos[1].flags = 0;
deviceQueueCreateInfos[1].queueFamilyIndex = transferQueueIndex;
deviceQueueCreateInfos[1].queueCount = 1l;
deviceQueueCreateInfos[1].pQueuePriorities = transferPriorities;
VkPhysicalDeviceFeatures vkPhysicalDeviceFeatures{};
vkGetPhysicalDeviceFeatures(vkPhysicalDevice, &vkPhysicalDeviceFeatures);
VkPhysicalDeviceFeatures requestedFeatures{}; // {} to initialize everything to 0
requestedFeatures.samplerAnisotropy = VK_TRUE;
requestedFeatures.sampleRateShading = VK_TRUE;
requestedFeatures.fillModeNonSolid = vkPhysicalDeviceFeatures.fillModeNonSolid;
VkDeviceCreateInfo vkDeviceCreateInfo{};
vkDeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
vkDeviceCreateInfo.flags = 0;
vkDeviceCreateInfo.pNext = nullptr;
vkDeviceCreateInfo.queueCreateInfoCount = 2;
vkDeviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfos;
vkDeviceCreateInfo.enabledExtensionCount = REQUIRED_EXTENSIONS.size();
vkDeviceCreateInfo.ppEnabledExtensionNames = REQUIRED_EXTENSIONS.data();
vkDeviceCreateInfo.pEnabledFeatures = &requestedFeatures;
if (ENABLE_VALIDATION_LAYERS) {
vkDeviceCreateInfo.enabledLayerCount = enabledLayerNames.size();
vkDeviceCreateInfo.ppEnabledLayerNames = enabledLayerNames.data();
}
result = vkCreateDevice(vkPhysicalDevice, &vkDeviceCreateInfo, vkAllocator, &vkDevice);
if (result != VK_SUCCESS) {
printf("Failed to create VkInstance! : %d\n", result);
throw result;
}
}
}
void CreateWindow(PKEWindowProperties *wp) {
if (vkInstance != nullptr) return;
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
window = glfwCreateWindow(wp->width, wp->height, "Pikul", nullptr, nullptr);
InitVulkan();
}
void DestroyWindow() {
if (vkInstance == nullptr) return;
if (VULKAN_DEBUG_REPORT) {
auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkDestroyDebugReportCallbackEXT");
vkDestroyDebugReportCallbackEXT(vkInstance, vkDebugReport, nullptr);
}
vkDestroySurfaceKHR(vkInstance, vkSurfaceKHR, vkAllocator);
vkDestroyDevice(vkDevice, vkAllocator);
vkDestroyInstance(vkInstance, vkAllocator);
glfwDestroyWindow(window);
glfwTerminate();
}
|