-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
548 lines (429 loc) · 18.6 KB
/
main.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
#include "vulkan/vk_command_buffer.h"
#include "vulkan/vk_command_pool.h"
#include "vulkan/vk_frame_buffer.h"
#include "vulkan/vk_render_pass.h"
#include <stddef.h>
#include <stdint.h>
#include <vulkan/vk_platform.h>
#include <vulkan/vulkan_core.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "debug/print.h"
#include "option.h"
#include "vulkan/vk_validation_layer.h"
#include "vulkan/vk_debug_messenger.h"
#include "vulkan/vk_queue_family.h"
#include "vulkan/vk_swap_chain.h"
#include "vulkan/vk_image_view.h"
#include "vulkan/vk_logical_device.h"
#include "vulkan/vk_instance_extension.h"
#include "vulkan/vk_physical_device.h"
#include "vulkan/vk_graphics_pipeline.h"
#include "vulkan/vk_swap_chain.h"
#include "vulkan/vk_vertex_data.h"
#include "utils/array.h"
#include "datastructures/list.h"
#define foreach(item, list) \
for(typeof(list[0]) *item = list; item < (&list)[1]; item++)
// Enable Validation Layers only in Debug Mode
#ifdef DEBUG
const bool ENABLE_VALIDATION_LAYERS = true;
#else
const bool ENABLE_VALIDATION_LAYERS = false;
#endif
// Window Size
static const uint32_t WIDTH = 800;
static const uint32_t HEIGHT = 600;
static GLFWwindow *p_window;
// Validation Layers to request/enable
static const char *VALIDATION_LAYERS[] = {
"VK_LAYER_KHRONOS_validation"
};
// Device Extensions to enable
static const char *DEVICE_EXTENSIONS[] = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
static const int MAX_FRAMES_IN_FLIGHT = 2;
// Handle to the Vulkan library instance
static VkInstance instance;
// Handle to the physical device(gpu) to use
// This object will be implicitly destroyed when the VkInstance is destroyed
static VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
// Handle to the logical device used for interfacing the physical device
static VkDevice device;
// Handle to the graphics queue
// Device queues are implicitly cleaned up when
// the device is destroyed
static VkQueue graphicsQueue;
// Handle to the present queue
static VkQueue presentQueue;
static struct SwapChainDetails swapChainDetails;
// Handle to the swap chain image views
static VkImageView *swapChainImageViews;
// Handle to the Debug callback
static VkDebugUtilsMessengerEXT debugMessenger;
// Handle to the window surface
static VkSurfaceKHR surface;
static VkRenderPass renderPass;
static struct GraphicsPipelineDetails graphicsPipelineDetails;
static VkBuffer vertexBuffer;
static VkDeviceMemory vertexBufferMemory;
static VkFramebuffer *swapChainFramebuffers;
static VkCommandPool commandPool;
static VkCommandBuffer *commandBuffers;
static VkSemaphore *imageAvailableSemaphores;
static VkSemaphore *renderFinishedSemaphores;
static VkFence *inFlightFences;
static uint32_t currentFrame = 0;
static bool frameBufferResized = false;
static List *vertices;
// Prototypes
static void create_vertex_buffer();
static void framebuffer_resize_callback(GLFWwindow *window, int width, int height)
{
frameBufferResized = true;
}
void init_window()
{
glfwInit();
// Tell glfw to not use OpenGL since we use Vulkan
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
p_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", NULL, NULL);
glfwSetFramebufferSizeCallback(p_window, framebuffer_resize_callback);
glfwSetWindowSizeLimits(p_window, 200, 200, GLFW_DONT_CARE, GLFW_DONT_CARE); // TODO: Doesn't work on wayfire (crashes when width or height is zero)
}
void create_instance()
{
// We specify some information about our application so that
// it can be used for optimizations. (Optional)
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle";
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;
// Here we tell the Vulkan Driver which Global Extensions and
// Validation Layers we want to use. (Required)
VkInstanceCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
// Get the extensions required to interface with the
// window system from GLFW.
// We also get additional extensions when
// validation layers are enabled.
struct RequiredExtensions requiredExtensions =
get_required_extensions();
createInfo.enabledExtensionCount = requiredExtensions.extension_count;
createInfo.ppEnabledExtensionNames = requiredExtensions.extensions;
// Add validation layers if enabled
if (ENABLE_VALIDATION_LAYERS) {
createInfo.enabledLayerCount = ARRAY_SIZE(VALIDATION_LAYERS);
createInfo.ppEnabledLayerNames = VALIDATION_LAYERS;
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = {};
// Add debug messenger to be used during
// vkCreateInstance and vkDestroyInstance
// The debug messenger will automatically
// be cleaned up afterwards
populate_debug_messenger_createinfo(&debugCreateInfo);
createInfo.pNext =
(VkDebugUtilsMessengerCreateInfoEXT*) &debugCreateInfo;
} else {
createInfo.enabledLayerCount = 0;
createInfo.pNext = NULL;
}
// Create the Vulkan instance
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
if (result != VK_SUCCESS) {
error("Failed to create Vulkan instance!\n");
exit(EXIT_FAILURE);
}
// Get the number of available extensions
uint32_t extension_count = 0;
vkEnumerateInstanceExtensionProperties(NULL, &extension_count, NULL);
// Get the details of the extensions
VkExtensionProperties extensions[extension_count];
vkEnumerateInstanceExtensionProperties(NULL,
&extension_count, extensions);
// Print the available extensions
printf("Available Extensions:\n");
for (size_t i = 0; i < extension_count; i++) {
printf("\t %s\n", extensions[i].extensionName);
}
// Check that the requested validation layers are available
if (ENABLE_VALIDATION_LAYERS &&
!check_validation_layer_support(VALIDATION_LAYERS,
ARRAY_SIZE(VALIDATION_LAYERS))) {
error("Validation layers requested, but not available!\n");
exit(EXIT_FAILURE);
}
}
void create_surface()
{
if (glfwCreateWindowSurface(instance, p_window, NULL, &surface)
!= VK_SUCCESS) {
error("Failed to create window surface!\n");
exit(EXIT_FAILURE);
}
}
static void create_sync_objects()
{
imageAvailableSemaphores = malloc(MAX_FRAMES_IN_FLIGHT);
renderFinishedSemaphores = malloc(MAX_FRAMES_IN_FLIGHT);
inFlightFences = malloc(MAX_FRAMES_IN_FLIGHT);
VkSemaphoreCreateInfo semaphoreInfo = {};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_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(device, &semaphoreInfo, NULL,
&imageAvailableSemaphores[i]) != VK_SUCCESS ||
vkCreateSemaphore(device, &semaphoreInfo, NULL,
&renderFinishedSemaphores[i]) != VK_SUCCESS ||
vkCreateFence(device, &fenceInfo, NULL,
&inFlightFences[i]) != VK_SUCCESS) {
error("Failed to create synchronization objects for a frame!");
exit(EXIT_FAILURE);
}
}
}
static void init_vulkan()
{
create_instance();
if (ENABLE_VALIDATION_LAYERS) {
setup_debug_messenger(instance, &debugMessenger);
}
create_surface();
pick_physical_device(
&instance,
&surface,
DEVICE_EXTENSIONS,
ARRAY_SIZE(DEVICE_EXTENSIONS),
&physicalDevice
);
if (create_logical_device(&physicalDevice, &surface,
DEVICE_EXTENSIONS,
ARRAY_SIZE(DEVICE_EXTENSIONS),
VALIDATION_LAYERS,
ARRAY_SIZE(VALIDATION_LAYERS),
&device)
!= VK_SUCCESS) {
error("Failed to create logical device!\n");
exit(EXIT_FAILURE);
}
struct QueueFamilyIndices queueFamilyIndices =
find_queue_families(physicalDevice, surface);
create_queue(&device,queueFamilyIndices.graphics_family.value,
&graphicsQueue);
create_queue(&device,queueFamilyIndices.present_family.value,
&presentQueue);
if(create_swap_chain(p_window, device, physicalDevice,
surface, &swapChainDetails)
!= VK_SUCCESS) {
error("Failed to create swap chain!\n");
exit(EXIT_FAILURE);
}
if (create_image_views(device,
swapChainDetails.images,
swapChainDetails.image_count,
&swapChainDetails.image_format,
&swapChainImageViews)
!= VK_SUCCESS) {
error("Failed to create image views!\n");
exit(EXIT_FAILURE);
}
renderPass =
create_render_pass(&device, &swapChainDetails.image_format);
graphicsPipelineDetails = create_graphics_pipeline(
&device, &swapChainDetails.extent, &renderPass);
if (create_frame_buffers(device,
&swapChainDetails,
swapChainImageViews,
&renderPass,
&swapChainFramebuffers) != VK_SUCCESS) {
error("Failed to create framebuffer!");
exit(EXIT_FAILURE);
}
commandPool = create_command_pool(&device, &physicalDevice, &surface);
create_vertex_buffer();
commandBuffers = create_command_buffer(&device, &commandPool, MAX_FRAMES_IN_FLIGHT);
create_sync_objects();
}
static uint32_t find_memory_type(
uint32_t type_filter,
VkMemoryPropertyFlags properties)
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
for (size_t i = 0; i < memProperties.memoryTypeCount; i++) {
if (type_filter & (1 << i) &&
(memProperties.memoryTypes[i].propertyFlags &
properties) == properties) {
return i;
}
}
error("Failed to find suitable memory type!");
exit(EXIT_FAILURE);
}
static void create_vertex_buffer()
{
Vertex vertex_data[] = {
{{-0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.5f}, {1.0f, 1.0f, 0.0f}},
{{-0.5f, 0.5f}, {1.0f, 0.0f, 1.0f}},
{{-0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}},
{{0.5f, 0.5f}, {1.0f, 1.0f, 0.0f}}
};
vertices = list_create(10);
for (size_t i = 0; i < ARRAY_SIZE(vertex_data); i++)
list_add(vertices, (void*)&vertex_data[i]);
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = sizeof(Vertex) * list_size(vertices);
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, NULL, &vertexBuffer)
!= VK_SUCCESS) {
error("Failed to create vertex buffer!");
exit(EXIT_FAILURE);
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, vertexBuffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex =
find_memory_type(memRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (vkAllocateMemory(device, &allocInfo, NULL, &vertexBufferMemory)
!= VK_SUCCESS) {
error("Failed to allocate vertex buffer memory!");
exit(EXIT_FAILURE);
}
vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);
void *data;
vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data);
memcpy(data, *list_get_elements(vertices), (size_t) bufferInfo.size);
vkUnmapMemory(device, vertexBufferMemory);
}
void draw_frame()
{
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(device, swapChainDetails.swap_chain, UINT64_MAX,
imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
recreate_swap_chain(p_window, device,
&swapChainImageViews, physicalDevice, surface,
&swapChainDetails, &renderPass,
&swapChainFramebuffers);
return;
} else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
error("Failed to acquire swap chain image!");
exit(EXIT_FAILURE);
}
// Only reset the fence if we are submitting work
vkResetFences(device, 1, &inFlightFences[currentFrame]);
vkResetCommandBuffer(commandBuffers[currentFrame], 0);
record_command_buffer(&renderPass, swapChainFramebuffers,
&swapChainDetails.extent,
&graphicsPipelineDetails.graphics_pipeline,
commandBuffers[currentFrame], imageIndex,
list_size(vertices), vertexBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
VkPipelineStageFlags waitStages[] = {
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
};
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[currentFrame];
VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame])
!= VK_SUCCESS) {
error("Failed to submit draw command buffer!");
exit(EXIT_FAILURE);
}
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { swapChainDetails.swap_chain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = NULL;
result = vkQueuePresentKHR(presentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR
|| frameBufferResized) {
frameBufferResized = false;
recreate_swap_chain(p_window, device,
&swapChainImageViews, physicalDevice, surface,
&swapChainDetails, &renderPass,
&swapChainFramebuffers);
} else if (result != VK_SUCCESS) {
error("Failed to present swap chain image!");
exit(EXIT_FAILURE);
}
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
static void main_loop()
{
while(!glfwWindowShouldClose(p_window)) {
glfwPollEvents();
draw_frame();
}
vkDeviceWaitIdle(device);
}
static void cleanup()
{
cleanup_swap_chain(device, swapChainDetails.swap_chain,
swapChainFramebuffers, swapChainImageViews,
swapChainDetails.image_count,
swapChainDetails.image_count);
vkDestroyBuffer(device, vertexBuffer, NULL);
vkFreeMemory(device, vertexBufferMemory, NULL);
vkDestroyPipeline(device,
graphicsPipelineDetails.graphics_pipeline, NULL);
vkDestroyPipelineLayout(device,
graphicsPipelineDetails.pipeline_layout, NULL);
vkDestroyRenderPass(device, renderPass, NULL);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vkDestroySemaphore(device, renderFinishedSemaphores[i], NULL);
vkDestroySemaphore(device, imageAvailableSemaphores[i], NULL);
vkDestroyFence(device, inFlightFences[i], NULL);
}
vkDestroyCommandPool(device, commandPool, NULL);
vkDestroyDevice(device, NULL);
if (ENABLE_VALIDATION_LAYERS) {
destroy_debug_messenger(instance, debugMessenger, NULL);
}
vkDestroySurfaceKHR(instance, surface, NULL);
vkDestroyInstance(instance, NULL);
glfwDestroyWindow(p_window);
glfwTerminate();
}
static void run()
{
init_window();
init_vulkan();
main_loop();
cleanup();
}
int main()
{
run();
return EXIT_SUCCESS;
}