-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-triangle.c
856 lines (696 loc) · 28.3 KB
/
hello-triangle.c
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
//
// hello-triangle.c
//
// Created by John Watson on 1/14/20.
//
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
#include <vulkan/vulkan.h>
typedef struct queue_family_indices {
uint32_t graphicsFamily;
bool didSetGraphicsFamily;
uint32_t presentFamily;
bool didSetPresentFamily;
} queue_family_indices_t;
typedef struct swapchain_support_details {
VkSurfaceCapabilitiesKHR capabilities;
VkSurfaceFormatKHR *formats;
uint32_t formatCount;
VkPresentModeKHR* presentModes;
uint32_t presentModeCount;
} swapchain_support_details_t;
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
const char *APP_NAME = "Test App";
const int WIDTH = 800;
const int HEIGHT = 600;
VkInstance vulkanInstance = VK_NULL_HANDLE;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice logicalDevice = VK_NULL_HANDLE;
VkQueue graphicsQueue = VK_NULL_HANDLE;
VkQueue presentQueue = VK_NULL_HANDLE;
VkSurfaceKHR vulkanSurface = VK_NULL_HANDLE;
queue_family_indices_t queueFamilyIndices;
VkSwapchainKHR swapChain;
VkImage *swapchainImages;
uint32_t swapchainImageCount = 0;
VkFormat swapchainImageFormat;
VkExtent2D swapchainExtent;
VkImageView *swapChainImageViews;
VkRenderPass renderPass;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
VkFramebuffer *swapchainFramebuffers;
VkCommandPool commandPool;
VkCommandBuffer *commandBuffers;
VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore;
const char *requiredExtensions[] = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};
const uint32_t requiredExtensionCount = 1;
char* GetResourcePath(char *filename) {
static char *basePath;
if (!basePath) {
basePath = SDL_GetBasePath();
}
size_t len = strlen(basePath) + strlen(filename);
char *path = malloc(len + 1);
sprintf(path, "%s%s", basePath, filename);
return path;
}
char* ReadBytesFromResource(char *name, long *sizeOut) {
char *path = GetResourcePath(name);
FILE *f = fopen(path, "rb");
free(path);
fseek(f, 0, SEEK_END);
long size = ftell(f);
if (sizeOut) {
*sizeOut = size;
}
rewind(f);
char *buffer = malloc(size);
fread(buffer, 1, size, f);
fclose(f);
return buffer;
}
#ifndef NDEBUG
#define FatalError(...) _FatalError(__FILE__, __LINE__, __VA_ARGS__)
#else
#define FatalError(...) _FatalError(NULL, -1, __VA_ARGS__);
#endif
void _FatalError(const char *file, int line, char *fmt, ...) {
fprintf(stderr, "FATAL ERROR");
if (line != -1) {
fprintf(stderr, " (%s:%d): ", file, line);
} else {
fprintf(stderr, ": ");
}
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
void InitVulkanInstance(SDL_Window *window) {
VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = APP_NAME,
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "No Engine",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_0,
};
uint32_t instanceExtensionCount;
SDL_Vulkan_GetInstanceExtensions(window, &instanceExtensionCount, NULL);
const char **extensionNames = calloc(instanceExtensionCount, sizeof(char*));
SDL_Vulkan_GetInstanceExtensions(window, &instanceExtensionCount, extensionNames);
VkInstanceCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &appInfo,
.enabledExtensionCount = instanceExtensionCount,
.ppEnabledExtensionNames = extensionNames,
.enabledLayerCount = 0,
};
VkResult result = vkCreateInstance(&createInfo, NULL, &vulkanInstance);
free(extensionNames);
if (result != VK_SUCCESS) {
FatalError("Failed to create instance");
}
}
bool IsDiscreteGPU(VkPhysicalDevice device) {
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
}
void PickPhysicalVulkanDevice(void) {
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(vulkanInstance, &deviceCount, NULL);
if (deviceCount == 0) {
FatalError("Failed to find Vulkan-capable GPU");
}
VkPhysicalDevice *devices = calloc(deviceCount, sizeof(VkPhysicalDevice));
vkEnumeratePhysicalDevices(vulkanInstance, &deviceCount, devices);
// Prefer discrete GPU, but accept integrated GPU.
for (size_t i = 0; i < deviceCount; i++) {
if (IsDiscreteGPU(devices[i])) {
physicalDevice = devices[i];
break;
}
}
if (physicalDevice == VK_NULL_HANDLE) {
printf("Discrete GPU not found, falling back to integrated GPU...\n");
physicalDevice = devices[0];
}
free(devices);
uint32_t extensionCount = 0;
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &extensionCount, NULL);
if (extensionCount == 0) {
FatalError("Returned zero available extensions for device.");
}
VkExtensionProperties *properties = calloc(extensionCount, sizeof(VkExtensionProperties));
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &extensionCount, properties);
for (uint32_t i = 0; i < requiredExtensionCount; i++) {
bool found = false;
for (uint32_t j = 0; j < extensionCount; j++) {
if (strcmp(requiredExtensions[i], properties[j].extensionName) == 0) {
found = true;
break;
}
}
if (!found) {
FatalError("Could not find required extension %s.", requiredExtensions[i]);
}
}
free(properties);
}
queue_family_indices_t FindQueueFamilies(void) {
queue_family_indices_t indices;
memset(&indices, 0, sizeof(queue_family_indices_t));
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, NULL);
if (queueFamilyCount == 0) {
return indices;
}
for (uint32_t i = 0; i < queueFamilyCount; i++) {
indices.graphicsFamily = i;
indices.didSetGraphicsFamily = true;
VkBool32 surfaceSupported = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, vulkanSurface, &surfaceSupported);
if (surfaceSupported) {
indices.presentFamily = i;
indices.didSetPresentFamily = true;
}
}
return indices;
}
void CreateLogicalDevice(void) {
queueFamilyIndices = FindQueueFamilies();
if (!queueFamilyIndices.didSetGraphicsFamily) {
FatalError("Failed to find graphics queue family.");
}
if (!queueFamilyIndices.didSetPresentFamily) {
FatalError("Failed to find present queue family.");
}
float queuePriority = 1.0f;
VkDeviceQueueCreateInfo queueCreateInfos[2] = {
{
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = queueFamilyIndices.graphicsFamily,
.queueCount = 1,
.pQueuePriorities = &queuePriority,
},
{
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = queueFamilyIndices.presentFamily,
.queueCount = 1,
.pQueuePriorities = &queuePriority,
},
};
// Use separate queues if graphics and present queue families are different.
const uint32_t queueCreateInfoCount = queueFamilyIndices.graphicsFamily == queueFamilyIndices.presentFamily ? 1 : 2;
VkPhysicalDeviceFeatures features = {};
VkDeviceCreateInfo deviceCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pQueueCreateInfos = queueCreateInfos,
.queueCreateInfoCount = queueCreateInfoCount,
.pEnabledFeatures = &features,
.enabledExtensionCount = 1,
.ppEnabledExtensionNames = requiredExtensions,
.enabledLayerCount = 0,
};
if (vkCreateDevice(physicalDevice, &deviceCreateInfo, NULL, &logicalDevice) != VK_SUCCESS) {
FatalError("Failed to create logical device.");
}
vkGetDeviceQueue(logicalDevice, queueFamilyIndices.graphicsFamily, 0, &graphicsQueue);
vkGetDeviceQueue(logicalDevice, queueFamilyIndices.presentFamily, 0, &presentQueue);
}
void CreateVulkanSurface(SDL_Window *window) {
if (!SDL_Vulkan_CreateSurface(window, vulkanInstance, &vulkanSurface)) {
FatalError("Failed to create Vulkan surface: %s", SDL_GetError());
}
}
swapchain_support_details_t QuerySwapchainSupport(void) {
swapchain_support_details_t details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, vulkanSurface, &details.capabilities);
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, vulkanSurface, &details.formatCount, NULL);
if (details.formatCount > 0) {
details.formats = calloc(details.formatCount, sizeof(VkSurfaceFormatKHR));
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, vulkanSurface, &details.formatCount, details.formats);
} else {
details.formats = NULL;
}
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, vulkanSurface, &details.presentModeCount, NULL);
if (details.presentModeCount > 0) {
details.presentModes = calloc(details.presentModeCount, sizeof(VkPresentModeKHR));
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, vulkanSurface, &details.presentModeCount, details.presentModes);
} else {
details.presentModes = NULL;
}
return details;
}
void FreeSwapchainSupportDetails(swapchain_support_details_t details) {
free(details.formats);
free(details.presentModes);
}
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(swapchain_support_details_t details) {
for (uint32_t i = 0; i < details.formatCount; i++) {
if (details.formats[i].format == VK_FORMAT_B8G8R8A8_UNORM && details.formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return details.formats[i];
}
}
return details.formats[0];
}
VkPresentModeKHR ChoosePresentMode(swapchain_support_details_t details) {
// Prefer VK_PRESENT_MODE_MAILBOX_KHR (triple buffering) but fall back to FIFO if not available.
for (uint32_t i = 0; i < details.presentModeCount; i++) {
if (details.presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
return details.presentModes[i];
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D ChooseSwapExtent(swapchain_support_details_t details) {
if (details.capabilities.currentExtent.width != UINT32_MAX) {
return details.capabilities.currentExtent;
}
VkExtent2D actualExtent = {
.width = WIDTH,
.height = HEIGHT,
};
actualExtent.width = MAX(details.capabilities.minImageExtent.width, MIN(details.capabilities.maxImageExtent.width, actualExtent.width));
actualExtent.height = MAX(details.capabilities.minImageExtent.height, MIN(details.capabilities.maxImageExtent.height, actualExtent.height));
return actualExtent;
}
void CreateSwapChain(swapchain_support_details_t details) {
VkSurfaceFormatKHR surfaceFormat = ChooseSwapSurfaceFormat(details);
VkPresentModeKHR presentMode = ChoosePresentMode(details);
VkExtent2D extent = ChooseSwapExtent(details);
uint32_t imageCount = details.capabilities.minImageCount + 1;
if (details.capabilities.maxImageCount > 0 && imageCount > details.capabilities.maxImageCount) {
imageCount = details.capabilities.maxImageCount;
}
VkSwapchainCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.surface = vulkanSurface,
.minImageCount = imageCount,
.imageFormat = surfaceFormat.format,
.imageColorSpace = surfaceFormat.colorSpace,
.imageExtent = extent,
.imageArrayLayers = 1,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.preTransform = details.capabilities.currentTransform,
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
.presentMode = presentMode,
.clipped = VK_TRUE,
.oldSwapchain = VK_NULL_HANDLE,
};
if (queueFamilyIndices.graphicsFamily != queueFamilyIndices.presentFamily) {
uint32_t indices[] = { queueFamilyIndices.graphicsFamily, queueFamilyIndices.presentFamily };
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = indices;
} else {
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = NULL;
}
if (vkCreateSwapchainKHR(logicalDevice, &createInfo, NULL, &swapChain) != VK_SUCCESS) {
FatalError("Failed to create swapchain.");
}
vkGetSwapchainImagesKHR(logicalDevice, swapChain, &swapchainImageCount, NULL);
swapchainImages = calloc(swapchainImageCount, sizeof(VkImage));
vkGetSwapchainImagesKHR(logicalDevice, swapChain, &swapchainImageCount, swapchainImages);
swapchainImageFormat = surfaceFormat.format;
swapchainExtent = extent;
}
void CreateImageViews(void) {
swapChainImageViews = calloc(swapchainImageCount, sizeof(VkImageView));
for (size_t i = 0; i < swapchainImageCount; i++) {
VkImageViewCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = swapchainImages[i],
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = swapchainImageFormat,
.components = {
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
.a = VK_COMPONENT_SWIZZLE_IDENTITY,
},
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
if (vkCreateImageView(logicalDevice, &createInfo, NULL, &swapChainImageViews[i]) != VK_SUCCESS) {
FatalError("Failed to create image views.");
}
}
}
VkShaderModule CreateShaderModule(const char *code, long size) {
VkShaderModuleCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = size,
.pCode = (const uint32_t *)code,
};
VkShaderModule shaderModule;
if (vkCreateShaderModule(logicalDevice, &createInfo, NULL, &shaderModule) != VK_SUCCESS) {
FatalError("Failed to create shader module.");
}
return shaderModule;
}
void CreateRenderPass(void) {
VkAttachmentDescription colorAttachment = {
.format = swapchainImageFormat,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
};
VkAttachmentReference colorAttachmentRef = {
.attachment = 0,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
VkSubpassDescription subpass = {
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentRef,
};
VkSubpassDependency dependency = {
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccessMask = 0,
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
};
VkRenderPassCreateInfo renderPassInfo = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &colorAttachment,
.subpassCount = 1,
.pSubpasses = &subpass,
.dependencyCount = 1,
.pDependencies = &dependency,
};
if (vkCreateRenderPass(logicalDevice, &renderPassInfo, NULL, &renderPass) != VK_SUCCESS) {
FatalError("Failed to create render pass.");
}
}
void CreateGraphicsPipeline(void) {
long vertexShaderCodeSize, fragmentShaderCodeSize;
char *vertexShaderCode = ReadBytesFromResource("vertex.spv", &vertexShaderCodeSize);
char *fragmentShaderCode = ReadBytesFromResource("fragment.spv", &fragmentShaderCodeSize);
VkShaderModule vertexShaderModule = CreateShaderModule(vertexShaderCode, vertexShaderCodeSize);
VkShaderModule fragmentShaderModule = CreateShaderModule(fragmentShaderCode, fragmentShaderCodeSize);
VkPipelineShaderStageCreateInfo vertexShaderStageInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = vertexShaderModule,
.pName = "main",
};
VkPipelineShaderStageCreateInfo fragmentShaderStageInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = fragmentShaderModule,
.pName = "main",
};
VkPipelineShaderStageCreateInfo shaderStages[] = { vertexShaderStageInfo, fragmentShaderStageInfo };
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = 0,
.pVertexBindingDescriptions = NULL,
.vertexAttributeDescriptionCount = 0,
.pVertexAttributeDescriptions = NULL,
};
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
.primitiveRestartEnable = VK_FALSE,
};
VkViewport viewport = {
.x = 0,
.y = 0,
.width = (float)swapchainExtent.width,
.height = (float)swapchainExtent.height,
.minDepth = 0,
.maxDepth = 1,
};
VkRect2D scissor = {
.offset = { .x = 0, .y = 0 },
.extent = swapchainExtent,
};
VkPipelineViewportStateCreateInfo viewportState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = 1,
.pViewports = &viewport,
.scissorCount = 1,
.pScissors = &scissor,
};
VkPipelineRasterizationStateCreateInfo rasterizer = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.depthClampEnable = VK_FALSE,
.rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL,
.lineWidth = 1,
.cullMode = VK_CULL_MODE_BACK_BIT,
.frontFace = VK_FRONT_FACE_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.depthBiasConstantFactor = 0,
.depthBiasClamp = 0,
.depthBiasSlopeFactor = 0,
};
VkPipelineMultisampleStateCreateInfo multisampling = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.sampleShadingEnable = VK_FALSE,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.minSampleShading = 1,
.pSampleMask = NULL,
.alphaToCoverageEnable = VK_FALSE,
.alphaToOneEnable = VK_FALSE,
};
VkPipelineColorBlendAttachmentState colorBlendAttachment = {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
.blendEnable = VK_FALSE,
.srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
.colorBlendOp = VK_BLEND_OP_ADD,
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
.alphaBlendOp = VK_BLEND_OP_ADD,
};
VkPipelineColorBlendStateCreateInfo colorBlending = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.logicOpEnable = VK_FALSE,
.logicOp = VK_LOGIC_OP_COPY,
.attachmentCount = 1,
.pAttachments = &colorBlendAttachment,
.blendConstants = { 0, 0, 0, 0 },
};
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 0,
.pSetLayouts = NULL,
.pushConstantRangeCount = 0,
.pPushConstantRanges = NULL,
};
if (vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, NULL, &pipelineLayout) != VK_SUCCESS) {
FatalError("Failed to create pipeline layout.");
}
VkGraphicsPipelineCreateInfo pipelineInfo = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.stageCount = 2,
.pStages = shaderStages,
.pVertexInputState = &vertexInputInfo,
.pInputAssemblyState = &inputAssembly,
.pViewportState = &viewportState,
.pMultisampleState = &multisampling,
.pDepthStencilState = NULL,
.pColorBlendState = &colorBlending,
.pRasterizationState = &rasterizer,
.pDynamicState = NULL,
.layout = pipelineLayout,
.renderPass = renderPass,
.subpass = 0,
.basePipelineHandle = VK_NULL_HANDLE,
.basePipelineIndex = -1,
};
if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &graphicsPipeline) != VK_SUCCESS) {
FatalError("Failed to create graphics pipeline.");
}
vkDestroyShaderModule(logicalDevice, fragmentShaderModule, NULL);
vkDestroyShaderModule(logicalDevice, vertexShaderModule, NULL);
}
void CreateFramebuffers(void) {
swapchainFramebuffers = calloc(swapchainImageCount, sizeof(VkFramebuffer));
for (size_t i = 0; i < swapchainImageCount; i++) {
VkImageView attachments[] = {
swapChainImageViews[i]
};
VkFramebufferCreateInfo framebufferInfo = {
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
.renderPass = renderPass,
.attachmentCount = 1,
.pAttachments = attachments,
.width = swapchainExtent.width,
.height = swapchainExtent.height,
.layers = 1,
};
if (vkCreateFramebuffer(logicalDevice, &framebufferInfo, NULL, &swapchainFramebuffers[i]) != VK_SUCCESS) {
FatalError("Failed to create framebuffer.");
}
}
}
void CreateCommandPool(void) {
VkCommandPoolCreateInfo poolInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = queueFamilyIndices.graphicsFamily,
.flags = 0,
};
if (vkCreateCommandPool(logicalDevice, &poolInfo, NULL, &commandPool) != VK_SUCCESS) {
FatalError("Failed to create command pool.");
}
}
void CreateCommandBuffers(void) {
commandBuffers = calloc(swapchainImageCount, sizeof(VkCommandBuffer));
VkCommandBufferAllocateInfo allocateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = commandPool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = swapchainImageCount,
};
if (vkAllocateCommandBuffers(logicalDevice, &allocateInfo, commandBuffers) != VK_SUCCESS) {
FatalError("Failed to allocate command buffers.");
}
for (size_t i = 0; i < swapchainImageCount; i++) {
VkCommandBufferBeginInfo beginInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = 0,
.pInheritanceInfo = NULL,
};
if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
FatalError("Failed to begin recording command buffer.");
}
VkClearValue clearColor = { 0.3f, 0.3f, 0.3f, 1.0f }; // Light grey background.
VkRenderPassBeginInfo renderPassInfo = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = renderPass,
.framebuffer = swapchainFramebuffers[i],
.renderArea = {
.offset = { .x = 0, .y = 0 },
.extent = swapchainExtent,
},
.clearValueCount = 1,
.pClearValues = &clearColor,
};
vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
vkCmdEndRenderPass(commandBuffers[i]);
if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
FatalError("Failed to record command buffer.");
}
}
}
void CreateSemaphores(void) {
VkSemaphoreCreateInfo semaphoreInfo = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
if (vkCreateSemaphore(logicalDevice, &semaphoreInfo, NULL, &imageAvailableSemaphore) != VK_SUCCESS) {
FatalError("Failed to create image available semaphore.");
}
if (vkCreateSemaphore(logicalDevice, &semaphoreInfo, NULL, &renderFinishedSemaphore) != VK_SUCCESS) {
FatalError("Failed to create render finished semaphore.");
}
}
void DrawFrame(void) {
uint32_t imageIndex;
vkAcquireNextImageKHR(logicalDevice, swapChain, UINT64_MAX, imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
VkSemaphore waitSemaphores[] = { imageAvailableSemaphore };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
VkSemaphore signalSemaphores[] = { renderFinishedSemaphore };
VkSubmitInfo submitInfo = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.waitSemaphoreCount = 1,
.pWaitSemaphores = waitSemaphores,
.pWaitDstStageMask = waitStages,
.commandBufferCount = 1,
.pCommandBuffers = &commandBuffers[imageIndex],
.signalSemaphoreCount = 1,
.pSignalSemaphores = signalSemaphores,
};
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS) {
FatalError("Failed to submit draw command buffer.");
}
VkSwapchainKHR swapchains[] = { swapChain };
VkPresentInfoKHR presentInfo = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.waitSemaphoreCount = 1,
.pWaitSemaphores = signalSemaphores,
.swapchainCount = 1,
.pSwapchains = swapchains,
.pImageIndices = &imageIndex,
};
vkQueuePresentKHR(presentQueue, &presentInfo);
}
int main(int argc, const char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(APP_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_VULKAN | SDL_WINDOW_ALLOW_HIGHDPI);
if (!window) {
FatalError("Failed to create window: %s", SDL_GetError());
}
InitVulkanInstance(window);
CreateVulkanSurface(window);
PickPhysicalVulkanDevice();
CreateLogicalDevice();
swapchain_support_details_t swapchainDetails = QuerySwapchainSupport();
if (swapchainDetails.formatCount == 0 || swapchainDetails.presentModeCount == 0) {
FatalError("No valid swapchain configuration found.");
}
CreateSwapChain(swapchainDetails);
FreeSwapchainSupportDetails(swapchainDetails);
CreateImageViews();
CreateRenderPass();
CreateGraphicsPipeline();
CreateFramebuffers();
CreateCommandPool();
CreateCommandBuffers();
CreateSemaphores();
DrawFrame();
SDL_Event event;
while (SDL_WaitEvent(&event)) {
if (event.type == SDL_QUIT) {
break;
}
}
vkDestroySemaphore(logicalDevice, renderFinishedSemaphore, NULL);
vkDestroySemaphore(logicalDevice, imageAvailableSemaphore, NULL);
vkDestroyCommandPool(logicalDevice, commandPool, NULL);
for (size_t i = 0; i < swapchainImageCount; i++) {
vkDestroyFramebuffer(logicalDevice, swapchainFramebuffers[i], NULL);
}
vkDestroyPipeline(logicalDevice, graphicsPipeline, NULL);
vkDestroyPipelineLayout(logicalDevice, pipelineLayout, NULL);
vkDestroyRenderPass(logicalDevice, renderPass, NULL);
for (size_t i = 0; i < swapchainImageCount; i++) {
vkDestroyImageView(logicalDevice, swapChainImageViews[i], NULL);
}
vkDestroySwapchainKHR(logicalDevice, swapChain, NULL);
vkDestroySurfaceKHR(vulkanInstance, vulkanSurface, NULL);
vkDestroyDevice(logicalDevice, NULL);
vkDestroyInstance(vulkanInstance, NULL);
SDL_Quit();
return 0;
}