-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle-glm.cpp
1022 lines (977 loc) · 47.9 KB
/
triangle-glm.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <chrono>
#include <vector>
#include <set>
#include <string>
#include <cstdio>
#include <stdexcept>
#include <optional>
#include <limits>
#include <algorithm>
#include <fstream>
#pragma warning(disable : 26812) // Disable enum class warning from Vulkan enums
const uint32_t WIN_WIDTH = 800;
const uint32_t WIN_HEIGHT = 800;
const int MAX_FRAMES_IN_FLIGHT = 2;
const std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
uint32_t currentFrame = 0;
bool framebufferResized = false;
GLFWwindow* window;
VkInstance instance;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice logicalDevice;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkSurfaceKHR surface;
VkSwapchainKHR swapchain;
VkFormat swapchainImageFormat;
VkExtent2D swapchainExtent;
std::vector<VkImage> swapchainImages;
std::vector<VkImageView> swapchainImageViews;
std::vector<VkFramebuffer> swapchainFramebuffers;
VkRenderPass renderPass;
VkPipelineLayout pipelineLayout;
VkDescriptorSetLayout descriptorSetLayout;
VkPipeline graphicsPipeline;
VkCommandPool commandPool;
VkDescriptorPool descriptorPool;
std::vector<VkDescriptorSet> descriptorSets;
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
std::vector<VkBuffer> uniformBuffers;
std::vector<VkDeviceMemory> uniformBuffersMemory;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores;
std::vector<VkSemaphore> renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
struct QueueFamilyIndices {
std::optional<uint32_t> graphicsFamily = std::nullopt;
std::optional<uint32_t> presentFamily = std::nullopt;
bool isComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); }
};
struct SwapchainSupportDetails {
VkSurfaceCapabilitiesKHR capabilities{};
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
struct Vertex {
glm::vec3 pos;
glm::vec3 normal;
glm::vec4 color;
static VkVertexInputBindingDescription getBindingDescription() {
VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions() {
std::vector<VkVertexInputAttributeDescription> attributeDescriptions(3);
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, pos);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, normal);
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, color);
return attributeDescriptions;
}
};
struct UniformBufferObject {
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
};
const std::vector<Vertex> vertices = {
{{-1, -1, 1}, {0, 0, 1}, {1, 0, 0, 1}},
{{1, -1, 1}, {0, 0, 1}, {1, 0, 0, 1}},
{{1, 1, 1}, {0, 0, 1}, {1, 0, 0, 1}},
{{-1, 1, 1}, {0, 0, 1}, {1, 0, 0, 1}},
{{-1, -1, -1}, {0, 0, -1}, {0, 1, 0, 1}},
{{1, -1, -1}, {0, 0, -1}, {0, 1, 0, 1}},
{{1, 1, -1}, {0, 0, -1}, {0, 1, 0, 1}},
{{-1, 1, -1}, {0, 0, -1}, {0, 1, 0, 1}},
{{-1, -1, 1}, {-1, 0, 0}, {0, 0, 1, 1}},
{{-1, -1, -1}, {-1, 0, 0}, {0, 0, 1, 1}},
{{-1, 1, -1}, {-1, 0, 0}, {0, 0, 1, 1}},
{{-1, 1, 1}, {-1, 0, 0}, {0, 0, 1, 1}},
{{1, -1, 1}, {1, 0, 0}, {1, 1, 0, 1}},
{{1, -1, -1}, {1, 0, 0}, {1, 1, 0, 1}},
{{1, 1, -1}, {1, 0, 0}, {1, 1, 0, 1}},
{{1, 1, 1}, {1, 0, 0}, {1, 1, 0, 1}},
{{-1, 1, 1}, {0, 1, 0}, {0, 1, 1, 1}},
{{1, 1, 1}, {0, 1, 0}, {0, 1, 1, 1}},
{{1, 1, -1}, {0, 1, 0}, {0, 1, 1, 1}},
{{-1, 1, -1}, {0, 1, 0}, {0, 1, 1, 1}},
{{-1, -1, 1}, {0, -1, 0}, {1, 0, 1, 1}},
{{1, -1, 1}, {0, -1, 0}, {1, 0, 1, 1}},
{{1, -1, -1}, {0, -1, 0}, {1, 0, 1, 1}},
{{-1, -1, -1}, {0, -1, 0}, {1, 0, 1, 1}}
};
const std::vector<uint16_t> indices = {
0, 1, 2, 2, 3, 0, // front
6, 5, 4, 4, 7, 6, // back
10, 9, 8, 8, 11, 10, // left
12, 13, 14, 14, 15, 12, // right
16, 17, 18, 18, 19, 16, // top
22, 21, 20, 20, 23, 22 // bottom
};
static std::vector<char> readBinaryFile(const std::string& filename) {
std::ifstream file;
file.open(filename, std::ios::ate | std::ios::binary);
if (!file.is_open())
throw std::runtime_error("Failed to open file '" + filename + "'!");
size_t fileSize = (size_t) file.tellg();
std::vector<char> buf(fileSize);
file.seekg(0);
file.read(buf.data(), fileSize);
file.close();
return buf;
}
void kbdCallback(GLFWwindow* w, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_Q || key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(w, true);
}
void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
framebufferResized = true;
}
QueueFamilyIndices getQueueFamilies(VkPhysicalDevice device) {
QueueFamilyIndices queueFamilyIndices;
// Retrieve queue families for device
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
if (queueFamilyCount == 0)
return queueFamilyIndices;
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
// Check for queue families with graphics and presentation support
for (uint32_t i = 0; i < queueFamilyCount; i++) {
if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
queueFamilyIndices.graphicsFamily = std::make_optional(i);
VkBool32 present = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &present);
if (present)
queueFamilyIndices.presentFamily = std::make_optional(i);
}
return queueFamilyIndices;
}
SwapchainSupportDetails querySwapchainSupport(VkPhysicalDevice device) {
SwapchainSupportDetails details;
// Query device surface capabilities
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
// Query device surface formats
uint32_t formatCounts;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCounts, nullptr);
if (formatCounts != 0) {
details.formats.resize(formatCounts);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCounts, details.formats.data());
}
// Query device presentation modes
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
if (presentModeCount != 0) {
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
}
return details;
}
bool isDeviceSuitable(VkPhysicalDevice device) {
// Check if device has queue family with graphics and present capabilities
QueueFamilyIndices queueFamilyIndices = getQueueFamilies(device);
// Check if device supports all required extensions (from deviceExtensions)
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
for (const VkExtensionProperties& extension : availableExtensions)
requiredExtensions.erase(extension.extensionName);
// Check if swapchain adequate
bool adequateSwapChain = false;
if (requiredExtensions.empty()) {
SwapchainSupportDetails swapchainSupport = querySwapchainSupport(device);
adequateSwapChain = !swapchainSupport.formats.empty() && !swapchainSupport.presentModes.empty();
}
return queueFamilyIndices.isComplete() && requiredExtensions.empty() && adequateSwapChain;
}
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
return i;
throw std::runtime_error("Failed to find suitable memory type!");
}
VkShaderModule createShaderModule(const std::vector<char>& code) {
// Create shader module creation info struct
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = (const uint32_t*)code.data();
// Create shader module
VkShaderModule shaderModule;
VkResult res = vkCreateShaderModule(logicalDevice, &createInfo, nullptr, &shaderModule);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create shader module!");
return shaderModule;
}
void createVkInstance() {
// Create application info struct
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Vulkan Playground";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
// Get instance extensions required by GLFW
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
printf("GLFW Required Extensions:\n");
for (uint32_t i = 0; i < glfwExtensionCount; i++) printf("\t%s\n", glfwExtensions[i]);
// Get available extensions
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
printf("Available extensions\n");
for (const VkExtensionProperties& ext : extensions) printf("\t%s\n", ext.extensionName);
// Create instance creation info struct
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
createInfo.enabledLayerCount = 0;
// Create instance
VkResult res = vkCreateInstance(&createInfo, nullptr, &instance);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create vulkan instance!");
}
void selectPhysicalDevice() {
// Get number of physical devices
uint32_t device_count = 0;
vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
if (device_count == 0)
throw std::runtime_error("Couldn't find any GPUs with Vulkan support!");
// Enumerate all found physical devices
std::vector<VkPhysicalDevice> devices(device_count);
vkEnumeratePhysicalDevices(instance, &device_count, devices.data());
// Pick a suitable device
for (const VkPhysicalDevice device : devices)
if (isDeviceSuitable(device)) { physicalDevice = device; break; }
if (physicalDevice == VK_NULL_HANDLE)
throw std::runtime_error("Failed to find a suitable GPU!");
// Retrieve device properties, report selected device
VkPhysicalDeviceProperties device_props;
vkGetPhysicalDeviceProperties(physicalDevice, &device_props);
printf("Selected GPU '%s'.\n", device_props.deviceName);
}
void createLogicalDevice() {
QueueFamilyIndices queueFamilyIndices = getQueueFamilies(physicalDevice);
float queuePriority = 1.0f;
// Create graphics queue creation info struct
VkDeviceQueueCreateInfo gfxQueueCreateInfo{};
gfxQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
gfxQueueCreateInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
gfxQueueCreateInfo.queueCount = 1;
gfxQueueCreateInfo.pQueuePriorities = &queuePriority;
// Create present queue creation info struct
VkDeviceQueueCreateInfo prsQueueCreateInfo{};
prsQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
prsQueueCreateInfo.queueFamilyIndex = queueFamilyIndices.presentFamily.value();
prsQueueCreateInfo.queueCount = 1;
prsQueueCreateInfo.pQueuePriorities = &queuePriority;
// Create device features struct
VkPhysicalDeviceFeatures deviceFeatures{};
// Create logical device creation info struct
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos{ gfxQueueCreateInfo, prsQueueCreateInfo };
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.queueCreateInfoCount = (uint32_t)queueCreateInfos.size();
createInfo.pEnabledFeatures = &deviceFeatures;
createInfo.enabledExtensionCount = (uint32_t)deviceExtensions.size();
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
createInfo.enabledLayerCount = 0;
VkResult res = vkCreateDevice(physicalDevice, &createInfo, nullptr, &logicalDevice);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create logical device!");
// Get graphics queue
vkGetDeviceQueue(logicalDevice, queueFamilyIndices.graphicsFamily.value(), 0, &graphicsQueue);
vkGetDeviceQueue(logicalDevice, queueFamilyIndices.presentFamily.value(), 0, &presentQueue);
}
void createSurface() {
VkResult res = glfwCreateWindowSurface(instance, window, nullptr, &surface);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create window surface!");
}
void createSwapchain() {
SwapchainSupportDetails swapChainSupport = querySwapchainSupport(physicalDevice);
// Select swapchain format
VkSurfaceFormatKHR surfaceFormat = swapChainSupport.formats[0];
for (const VkSurfaceFormatKHR availableFormat : swapChainSupport.formats)
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB
&& availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
surfaceFormat = availableFormat;
// Select presentation mode
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
for (const VkPresentModeKHR availablePresentMode : swapChainSupport.presentModes)
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
presentMode = availablePresentMode;
// Select swap extent
VkExtent2D extent = swapChainSupport.capabilities.currentExtent;
if (extent.width == std::numeric_limits<uint32_t>::max()) {
int width, height;
glfwGetWindowSize(window, &width, &height);
extent.width = std::clamp((uint32_t)width, swapChainSupport.capabilities.minImageExtent.width, swapChainSupport.capabilities.maxImageExtent.width);
extent.height = std::clamp((uint32_t)height, swapChainSupport.capabilities.minImageExtent.height, swapChainSupport.capabilities.maxImageExtent.height);
}
// Query reasonable image count
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount)
imageCount = swapChainSupport.capabilities.maxImageCount;
// Create swapchain creation info struct
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = surface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = surfaceFormat.format;
createInfo.imageColorSpace = surfaceFormat.colorSpace;
createInfo.imageExtent = extent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE;
// Check queue indices for sharing mode
QueueFamilyIndices indices = getQueueFamilies(physicalDevice);
if (indices.graphicsFamily != indices.presentFamily) {
uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
} else {
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
}
// Create swapchain
VkResult res = vkCreateSwapchainKHR(logicalDevice, &createInfo, nullptr, &swapchain);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create swapchain!");
// Retrieve handles to swapchain images
vkGetSwapchainImagesKHR(logicalDevice, swapchain, &imageCount, nullptr);
swapchainImages.resize(imageCount);
vkGetSwapchainImagesKHR(logicalDevice, swapchain, &imageCount, swapchainImages.data());
// Save swapchain image format and extent
swapchainImageFormat = surfaceFormat.format;
swapchainExtent = extent;
}
void createImageViews() {
// Reserve space for image views
swapchainImageViews.resize(swapchainImages.size());
// Create swapchain image views
for (size_t i = 0; i < swapchainImages.size(); i++) {
VkImageViewCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = swapchainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = swapchainImageFormat;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
VkResult res = vkCreateImageView(logicalDevice, &createInfo, nullptr, &swapchainImageViews[i]);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create swapchain image view!");
}
}
void createRenderPass() {
// Create render pass color attachment description
VkAttachmentDescription colorAttachment{};
colorAttachment.format = swapchainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
// Create render pass color attachment reference
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
// Create subpass dependencies
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
// Create render pass info struct
VkRenderPassCreateInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
VkResult res = vkCreateRenderPass(logicalDevice, &renderPassInfo, nullptr, &renderPass);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create render pass!");
}
void createDescriptorSetLayout() {
// Set up descriptor set layout binding struct
VkDescriptorSetLayoutBinding uboLayoutBinding{};
uboLayoutBinding.binding = 0;
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uboLayoutBinding.descriptorCount = 1;
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
// Create descriptor set layout
VkDescriptorSetLayoutCreateInfo layoutInfo{};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &uboLayoutBinding;
VkResult res = vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &descriptorSetLayout);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create descriptor set layout!");
}
void createGraphicsPipeline() {
VkResult res;
// Read shader code from spv files
std::vector<char> vertShaderCode = readBinaryFile("shaders/spv/triangle-vert.spv");
std::vector<char> fragShaderCode = readBinaryFile("shaders/spv/triangle-frag.spv");
// Create shader modules from code
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
// Create shader pipeline stage info structs
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
// Create pipeline vertex input state info struct
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
VkVertexInputBindingDescription bindingDescription = Vertex::getBindingDescription();
std::vector<VkVertexInputAttributeDescription> attributeDescriptions = Vertex::getAttributeDescriptions();
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.vertexAttributeDescriptionCount = (uint32_t)attributeDescriptions.size();
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
// Create pipeline input assembly state info struct
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
// Create viewport info
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = (float) swapchainExtent.width;
viewport.height = (float) swapchainExtent.height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
// Create scissor rectangle
VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = swapchainExtent;
// Create viewport state from viewport and scissor rectangle
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
// Create rasterizer state info struct
VkPipelineRasterizationStateCreateInfo rasterizer{};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
// Create multisampler state info struct
VkPipelineMultisampleStateCreateInfo multisampler{};
multisampler.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampler.sampleShadingEnable = VK_FALSE;
multisampler.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
// Create color blend attachment state info struct
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;
// Create color blend state info struct
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
// Create pipeline uniform layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
res = vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create pipeline layout!");
// Create graphics pipeline info struct
VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampler;
pipelineInfo.pDepthStencilState = nullptr;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = nullptr;
pipelineInfo.layout = pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
pipelineInfo.basePipelineIndex = -1;
// Create graphics pipeline
res = vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create graphics pipeline!");
// Clean up shader modules after graphics pipeline created
vkDestroyShaderModule(logicalDevice, vertShaderModule, nullptr);
vkDestroyShaderModule(logicalDevice, fragShaderModule, nullptr);
}
void createFramebuffers() {
swapchainFramebuffers.resize(swapchainImageViews.size());
for (size_t i = 0; i < swapchainImageViews.size(); i++) {
VkImageView attachments[] = { swapchainImageViews[i] };
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = renderPass;
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = swapchainExtent.width;
framebufferInfo.height = swapchainExtent.height;
framebufferInfo.layers = 1;
VkResult res = vkCreateFramebuffer(logicalDevice, &framebufferInfo, nullptr, &swapchainFramebuffers[i]);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create framebuffer!");
}
}
void createCommandPool() {
QueueFamilyIndices queueFamilyIndices = getQueueFamilies(physicalDevice);
VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
VkResult res = vkCreateCommandPool(logicalDevice, &poolInfo, nullptr, &commandPool);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create command pool!");
}
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
VkResult res;
// Create buffer
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
res = vkCreateBuffer(logicalDevice, &bufferInfo, nullptr, &buffer);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create buffer!");
// Get memory requirements of buffer
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(logicalDevice, buffer, &memRequirements);
// Allocate buffer memory
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
res = vkAllocateMemory(logicalDevice, &allocInfo, nullptr, &bufferMemory);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to allocate buffer memory!");
// Bind memory to buffer
vkBindBufferMemory(logicalDevice, buffer, bufferMemory, 0);
}
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
// Allocate command buffer for memory transfer
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = commandPool;
allocInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
vkAllocateCommandBuffers(logicalDevice, &allocInfo, &commandBuffer);
// Begin writing to command buffer
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
// Dispatch copy command to command buffer
VkBufferCopy copyRegion{};
copyRegion.size = size;
vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region);
// End writing to command buffer
vkEndCommandBuffer(commandBuffer);
// Submit command buffer to graphics queue
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
vkFreeCommandBuffers(logicalDevice, commandPool, 1, &commandBuffer);
}
void createVertexBuffer() {
VkDeviceSize bufferSize = sizeof(Vertex) * vertices.size();
// Create staging buffer visible to host
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// Copy vertices to staging buffer as transfer source buffer, with host visible and coherent memory
void* data;
vkMapMemory(logicalDevice, stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, vertices.data(), (size_t)bufferSize);
vkUnmapMemory(logicalDevice, stagingBufferMemory);
// Create vertex buffer as transfer destination buffer, with device local memory
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
// Copy from staging to vertex buffer
copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
// Destroy staging buffer and free memory
vkDestroyBuffer(logicalDevice, stagingBuffer, nullptr);
vkFreeMemory(logicalDevice, stagingBufferMemory, nullptr);
}
void createIndexBuffer() {
VkDeviceSize bufferSize = sizeof(uint16_t) * indices.size();
// Create staging buffer visible to host
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// Copy indices to staging buffer as transfer soruce buffer, with host visible and coherent memory
void* data;
vkMapMemory(logicalDevice, stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, indices.data(), (size_t)bufferSize);
vkUnmapMemory(logicalDevice, stagingBufferMemory);
// Create index buffer as transfer destination buffer, with device local memory
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
// Copy from staging to index buffer
copyBuffer(stagingBuffer, indexBuffer, bufferSize);
// Destroy staging buffer and free memory
vkDestroyBuffer(logicalDevice, stagingBuffer, nullptr);
vkFreeMemory(logicalDevice, stagingBufferMemory, nullptr);
}
void createUniformBuffers() {
VkDeviceSize bufferSize = sizeof(UniformBufferObject);
uniformBuffers.resize(MAX_FRAMES_IN_FLIGHT);
uniformBuffersMemory.resize(MAX_FRAMES_IN_FLIGHT);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBuffers[i], uniformBuffersMemory[i]);
}
void createDescriptorPool() {
VkDescriptorPoolSize poolSize{};
poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSize.descriptorCount = (uint32_t)MAX_FRAMES_IN_FLIGHT;
VkDescriptorPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.poolSizeCount = 1;
poolInfo.pPoolSizes = &poolSize;
poolInfo.maxSets = (uint32_t)MAX_FRAMES_IN_FLIGHT;
VkResult res = vkCreateDescriptorPool(logicalDevice, &poolInfo, nullptr, &descriptorPool);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create descriptor pool!");
}
void allocateDescriptorSets() {
std::vector<VkDescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, descriptorSetLayout);
VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = (uint32_t)MAX_FRAMES_IN_FLIGHT;
allocInfo.pSetLayouts = layouts.data();
descriptorSets.resize(MAX_FRAMES_IN_FLIGHT);
VkResult res = vkAllocateDescriptorSets(logicalDevice, &allocInfo, descriptorSets.data());
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to allocate descriptor sets!");
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = uniformBuffers[i];
bufferInfo.offset = 0;
bufferInfo.range = sizeof(UniformBufferObject);
VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSets[i];
descriptorWrite.dstBinding = 0;
descriptorWrite.dstArrayElement = 0;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = &bufferInfo;
vkUpdateDescriptorSets(logicalDevice, 1, &descriptorWrite, 0, nullptr);
}
}
void createSyncObjects() {
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
VkSemaphoreCreateInfo semaphoreInfo{};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
VkFenceCreateInfo fenceInfo{};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
if (vkCreateSemaphore(logicalDevice, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS)
throw std::runtime_error("Failed to create image available semaphore!");
if (vkCreateSemaphore(logicalDevice, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS)
throw std::runtime_error("Failed to create render finished semaphore!");
if (vkCreateFence(logicalDevice, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS)
throw std::runtime_error("Failed to create in flight fence!");
}
}
void allocateCommandBuffers() {
commandBuffers.resize(MAX_FRAMES_IN_FLIGHT);
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = commandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
VkResult res = vkAllocateCommandBuffers(logicalDevice, &allocInfo, commandBuffers.data());
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to create command buffers!");
}
void recreateSwapchain() {
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
while (width == 0 || height == 0) {
glfwGetFramebufferSize(window, &width, &height);
glfwWaitEvents();
}
// Wait for device to idle
vkDeviceWaitIdle(logicalDevice);
// Clean up previous swapchain related resources
for (size_t i = 0; i < swapchainFramebuffers.size(); i++)
vkDestroyFramebuffer(logicalDevice, swapchainFramebuffers[i], nullptr);
vkDestroyPipeline(logicalDevice, graphicsPipeline, nullptr);
vkDestroyPipelineLayout(logicalDevice, pipelineLayout, nullptr);
vkDestroyRenderPass(logicalDevice, renderPass, nullptr);
for (size_t i = 0; i < swapchainImageViews.size(); i++)
vkDestroyImageView(logicalDevice, swapchainImageViews[i], nullptr);
vkDestroySwapchainKHR(logicalDevice, swapchain, nullptr);
for (size_t i = 0; i < uniformBuffers.size(); i++) {
vkDestroyBuffer(logicalDevice, uniformBuffers[i], nullptr);
vkFreeMemory(logicalDevice, uniformBuffersMemory[i], nullptr);
}
vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr);
// Recreate swapchain and dependent resources
createSwapchain();
createImageViews();
createRenderPass();
createGraphicsPipeline();
createFramebuffers();
createUniformBuffers();
createDescriptorPool();
allocateDescriptorSets();
allocateCommandBuffers();
}
void updateUniformBuffer(uint32_t currentImage) {
static auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
// Create UBO
UniformBufferObject ubo{};
glm::mat4 model(1.0f);
model *= glm::scale(glm::mat4(1.0f), glm::vec3(0.5f, 0.5f, 0.5f));
model *= glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
model *= glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
ubo.model = model;
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.proj = glm::perspective(glm::radians(45.0f), swapchainExtent.width / (float) swapchainExtent.height, 0.1f, 10.0f);
ubo.proj[1][1] *= -1;
// Copy UBO to current uniform buffer
void* data;
vkMapMemory(logicalDevice, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
memcpy(data, &ubo, sizeof(ubo));
vkUnmapMemory(logicalDevice, uniformBuffersMemory[currentImage]);
}
void drawFrame() {
VkResult res;
// Wait for frame to stop being in flight
vkWaitForFences(logicalDevice, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
// Acquire next swapchain image
uint32_t imageIndex;
res = vkAcquireNextImageKHR(logicalDevice, swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
if (res == VK_ERROR_OUT_OF_DATE_KHR || res == VK_SUBOPTIMAL_KHR || framebufferResized) {
framebufferResized = false;
recreateSwapchain();
return;
} else if (res != VK_SUCCESS)
throw std::runtime_error("Failed to acquire swapchain image!");
// Reset in flight fences so next frame can begin rendering
vkResetFences(logicalDevice, 1, &inFlightFences[currentFrame]);
// Reset and record command buffer
vkResetCommandBuffer(commandBuffers[currentFrame], 0);
// Begin recording to command buffer
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = nullptr;
res = vkBeginCommandBuffer(commandBuffers[currentFrame], &beginInfo);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to begin recording to command buffer!");
VkClearValue clearColor = {{{ 0.0f, 0.0f, 0.0f, 1.0f }}};
// Setup render pass
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderPass;
renderPassInfo.framebuffer = swapchainFramebuffers[imageIndex];
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = swapchainExtent;
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
// Begin render pass
vkCmdBeginRenderPass(commandBuffers[currentFrame], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// Bind graphics pipeline
vkCmdBindPipeline(commandBuffers[currentFrame], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
// Bind vertex buffer
VkBuffer vertexBuffers[] = { vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(commandBuffers[currentFrame], 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffers[currentFrame], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindDescriptorSets(commandBuffers[currentFrame], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[currentFrame], 0, nullptr);
// Draw vertices
vkCmdDrawIndexed(commandBuffers[currentFrame], (uint32_t)indices.size(), 1, 0, 0, 0);
// End render pass
vkCmdEndRenderPass(commandBuffers[currentFrame]);
res = vkEndCommandBuffer(commandBuffers[currentFrame]);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to finalize recording command buffer!");
updateUniformBuffer(currentFrame);
// Submit command buffer
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[currentFrame];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
res = vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]);
if (res != VK_SUCCESS)
throw std::runtime_error("Failed to submit draw command buffer!");
// Present result to swapchain
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapchain;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr;
vkQueuePresentKHR(presentQueue, &presentInfo);
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
int main() {
// GLFW setup
if (!glfwInit()) { printf("Error initializing GLFW! Exiting...\n"); return 1; }
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "Vulkan Playground", nullptr, nullptr);
if (!window) { printf("Failed to create GLFW window! Exiting...\n"); glfwTerminate(); return 1; }
glfwSetKeyCallback(window, kbdCallback);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
// Vulkan setup
createVkInstance();
createSurface();
selectPhysicalDevice();
createLogicalDevice();
createSwapchain();
createImageViews();
createRenderPass();
createDescriptorSetLayout();
createGraphicsPipeline();
createFramebuffers();
createCommandPool();
createVertexBuffer();
createIndexBuffer();
createUniformBuffers();
createDescriptorPool();
allocateDescriptorSets();
allocateCommandBuffers();
createSyncObjects();
// Render loop
while(!glfwWindowShouldClose(window)) {
drawFrame();
glfwPollEvents();
}
// Vulkan cleanup
vkDeviceWaitIdle(logicalDevice);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vkDestroySemaphore(logicalDevice, imageAvailableSemaphores[i], nullptr);
vkDestroySemaphore(logicalDevice, renderFinishedSemaphores[i], nullptr);
vkDestroyFence(logicalDevice, inFlightFences[i], nullptr);
vkDestroyBuffer(logicalDevice, uniformBuffers[i], nullptr);
vkFreeMemory(logicalDevice, uniformBuffersMemory[i], nullptr);
}
vkDestroyBuffer(logicalDevice, indexBuffer, nullptr);
vkFreeMemory(logicalDevice, indexBufferMemory, nullptr);
vkDestroyBuffer(logicalDevice, vertexBuffer, nullptr);
vkFreeMemory(logicalDevice, vertexBufferMemory, nullptr);
vkDestroyCommandPool(logicalDevice, commandPool, nullptr);
for (VkFramebuffer framebuffer : swapchainFramebuffers)
vkDestroyFramebuffer(logicalDevice, framebuffer, nullptr);
vkDestroyPipeline(logicalDevice, graphicsPipeline, nullptr);