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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
#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]))
const bool ENABLE_VALIDATION_LAYERS = true;
const bool VULKAN_DEBUG_REPORT = true;
/*
* Initialization
*/
GLFWwindow *window = nullptr;
VkInstance vkInstance = nullptr;
VkPhysicalDevice vkPhysicalDevice = nullptr;
VkDevice vkDevice = nullptr;
VkSurfaceKHR vkSurfaceKHR = nullptr;
VkDebugReportCallbackEXT vkDebugReport = nullptr;
VkAllocationCallbacks vkAllocatorStruct = {};
VkAllocationCallbacks *vkAllocator = nullptr;
unsigned int graphicsFamilyIndex;
unsigned int presentFamilyIndex;
/*
* Instantiation
*/
unsigned int CURRENT_FRAME = 0;
unsigned int selectedSurfaceIndex = -1u;
unsigned int selectedPresentIndex = -1u;
unsigned int swapchainLength = 0u;
VkSwapchainKHR vkSwapchainKHR = nullptr;
VkSurfaceFormatKHR vkSurfaceFormatKHR;
VkPresentModeKHR vkPresentModeKHR;
VkExtent2D extent;
VkImage *swapchainImages = nullptr;
VkImageView *swapchainImageViews = nullptr;
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;
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());
graphicsFamilyIndex = 0;
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 CreateSwapchain() {
VkSurfaceCapabilitiesKHR surfaceCapabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceCapabilities);
if (selectedSurfaceIndex == -1u) {
unsigned int surfaceFormatCounts;
vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceFormatCounts, nullptr);
VkSurfaceFormatKHR *surfaceFormats = pke::PkeNew<VkSurfaceFormatKHR>(sizeof(VkSurfaceFormatKHR) * surfaceFormatCounts);
vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceFormatCounts, surfaceFormats);
selectedSurfaceIndex = 0;
for (long i = 0; i < surfaceFormatCounts; ++i) {
if (surfaceFormats[i].format != VkFormat::VK_FORMAT_B8G8R8A8_SRGB) continue;
if (surfaceFormats[i].colorSpace != VkColorSpaceKHR::VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) continue;
selectedSurfaceIndex = i;
vkSurfaceFormatKHR.colorSpace = surfaceFormats[i].colorSpace;
vkSurfaceFormatKHR.format = surfaceFormats[i].format;
break;
}
pke::PkeDelete(surfaceFormats);
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
extent.width = width;
extent.height = height;
// clamp
width = extent.width < surfaceCapabilities.minImageExtent.width ? surfaceCapabilities.minImageExtent.width : extent.width;
extent.width = width > surfaceCapabilities.maxImageExtent.width ? surfaceCapabilities.maxImageExtent.width : width;
height = extent.height < surfaceCapabilities.minImageExtent.height ? surfaceCapabilities.minImageExtent.height : extent.height;
extent.height = height > surfaceCapabilities.maxImageExtent.height ? surfaceCapabilities.maxImageExtent.height : height;
if (selectedPresentIndex == -1u) {
unsigned int presentModeCount = 0;
vkGetPhysicalDeviceSurfacePresentModesKHR(vkPhysicalDevice, vkSurfaceKHR, &presentModeCount, nullptr);
VkPresentModeKHR *presentModes = pke::PkeNew<VkPresentModeKHR>(sizeof(VkPresentModeKHR) * presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(vkPhysicalDevice, vkSurfaceKHR, &presentModeCount, presentModes);
unsigned long mailboxIndex = -1;
unsigned long fifoRelaxedIndex = -1;
for (long i = 0; i < presentModeCount; ++i) {
if (presentModes[i] != VkPresentModeKHR::VK_PRESENT_MODE_MAILBOX_KHR) {
mailboxIndex = i;
} else if (presentModes[i] != VkPresentModeKHR::VK_PRESENT_MODE_FIFO_RELAXED_KHR) {
fifoRelaxedIndex = i;
}
}
selectedPresentIndex = mailboxIndex != -1ul ? mailboxIndex : fifoRelaxedIndex;
vkPresentModeKHR = presentModes[selectedPresentIndex];
pke::PkeDelete(presentModes);
}
VkSwapchainCreateInfoKHR vkSwapchainCreateInfo{};
vkSwapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
vkSwapchainCreateInfo.flags = 0;
vkSwapchainCreateInfo.minImageCount = MAX_FRAMES_IN_FLIGHT;
vkSwapchainCreateInfo.imageFormat = vkSurfaceFormatKHR.format;
vkSwapchainCreateInfo.imageColorSpace = vkSurfaceFormatKHR.colorSpace;
vkSwapchainCreateInfo.imageExtent = extent;
vkSwapchainCreateInfo.imageArrayLayers = 1;
vkSwapchainCreateInfo.imageUsage = VkImageUsageFlagBits::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
vkSwapchainCreateInfo.preTransform = surfaceCapabilities.currentTransform;
vkSwapchainCreateInfo.compositeAlpha = VkCompositeAlphaFlagBitsKHR::VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
vkSwapchainCreateInfo.presentMode = vkPresentModeKHR;
vkSwapchainCreateInfo.clipped = VK_TRUE;
vkSwapchainCreateInfo.oldSwapchain = vkSwapchainKHR;
vkSwapchainCreateInfo.surface = vkSurfaceKHR;
unsigned int qfi[2] = { graphicsFamilyIndex, presentFamilyIndex };
if (graphicsFamilyIndex != presentFamilyIndex) {
vkSwapchainCreateInfo.imageSharingMode = VkSharingMode::VK_SHARING_MODE_CONCURRENT;
vkSwapchainCreateInfo.queueFamilyIndexCount = 2;
vkSwapchainCreateInfo.pQueueFamilyIndices = qfi;
} else {
vkSwapchainCreateInfo.imageSharingMode = VkSharingMode::VK_SHARING_MODE_EXCLUSIVE;
vkSwapchainCreateInfo.queueFamilyIndexCount = 0;
}
vkCreateSwapchainKHR(vkDevice, &vkSwapchainCreateInfo, vkAllocator, &vkSwapchainKHR);
VkImageSubresourceRange vkImageSubresourceRange;
vkImageSubresourceRange.aspectMask = VkImageAspectFlagBits::VK_IMAGE_ASPECT_COLOR_BIT;
vkImageSubresourceRange.baseMipLevel = 0u;
vkImageSubresourceRange.levelCount = 1u;
vkImageSubresourceRange.baseArrayLayer = 0u;
vkImageSubresourceRange.layerCount = 1u;
VkImageViewCreateInfo vkImageViewCreateInfo{};
vkImageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
vkImageViewCreateInfo.flags = 0;
vkImageViewCreateInfo.viewType = VkImageViewType::VK_IMAGE_VIEW_TYPE_2D;
vkImageViewCreateInfo.format = vkSurfaceFormatKHR.format;
vkImageViewCreateInfo.components = {
VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
};
vkImageViewCreateInfo.subresourceRange = vkImageSubresourceRange;
vkGetSwapchainImagesKHR(vkDevice, vkSwapchainKHR, &swapchainLength, nullptr);
swapchainImages = pke::PkeNew<VkImage>(sizeof(VkImage) * swapchainLength);
vkGetSwapchainImagesKHR(vkDevice, vkSwapchainKHR, &swapchainLength, swapchainImages);
swapchainImageViews = pke::PkeNew<VkImageView>(sizeof(VkImageView) * swapchainLength);
for (long i = 0; i < swapchainLength; ++i) {
vkImageViewCreateInfo.image = swapchainImages[i];
vkCreateImageView(vkDevice, &vkImageViewCreateInfo, vkAllocator, &swapchainImageViews[i]);
}
}
void DestroySwapchain() {
if (swapchainImageViews!= nullptr && swapchainImageViews != reinterpret_cast<VkImageView *>(0xCAFEBABE)) {
for (long i = 0; i < swapchainLength; ++i) {
vkDestroyImageView(vkDevice, swapchainImageViews[i], vkAllocator);
}
pke::PkeDelete(swapchainImageViews);
pke::PkeDelete(swapchainImages);
}
vkDestroySwapchainKHR(vkDevice, vkSwapchainKHR, vkAllocator);
swapchainImages = reinterpret_cast<VkImage *>(0xCAFEBABE);
swapchainImageViews = reinterpret_cast<VkImageView *>(0xCAFEBABE);
}
void FramebufferResizeCallback(GLFWwindow *window, int width, int height) {
if (extent.width == width && extent.height != height) {
return;
}
DestroySwapchain();
CreateSwapchain();
}
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();
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
CreateSwapchain();
}
void DestroyWindow() {
if (vkInstance == nullptr) return;
if (VULKAN_DEBUG_REPORT) {
auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkDestroyDebugReportCallbackEXT");
vkDestroyDebugReportCallbackEXT(vkInstance, vkDebugReport, nullptr);
}
DestroySwapchain();
vkDestroySurfaceKHR(vkInstance, vkSurfaceKHR, vkAllocator);
vkDestroyDevice(vkDevice, vkAllocator);
vkDestroyInstance(vkInstance, vkAllocator);
glfwDestroyWindow(window);
glfwTerminate();
}
|