source: opengl-game/vulkan-game.cpp@ 69dccfe

feature/imgui-sdl points-test
Last change on this file since 69dccfe was 69dccfe, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Implement a translucent, fullscreen overlay that shows a loaded texture. This will later be used to show a UI rendered by SDL.

  • Property mode set to 100644
File size: 70.2 KB
Line 
1/*
2DESIGN GUIDE
3
4-I should store multiple buffers (e.g. vertex and index buffers) in the same VkBuffer and use offsets into it
5-For specifying a separate transform for each model, I can specify a descriptorCount > ` in the ubo layout binding
6-Name class instance variables that are pointers (and possibly other pointer variables as well) like pVarName
7*/
8
9#include "game-gui-glfw.hpp"
10
11#include "game-gui-sdl.hpp"
12
13//#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
14
15#define GLM_FORCE_RADIANS
16#define GLM_FORCE_DEPTH_ZERO_TO_ONE
17#include <glm/glm.hpp>
18#include <glm/gtc/matrix_transform.hpp>
19
20#define STB_IMAGE_IMPLEMENTATION
21#include "stb_image.h"
22
23#include <iostream>
24#include <fstream>
25#include <algorithm>
26#include <vector>
27#include <array>
28#include <set>
29#include <optional>
30#include <chrono>
31
32using namespace std;
33
34const int SCREEN_WIDTH = 800;
35const int SCREEN_HEIGHT = 600;
36
37const int MAX_FRAMES_IN_FLIGHT = 2;
38
39#ifdef NDEBUG
40 const bool enableValidationLayers = false;
41#else
42 const bool enableValidationLayers = true;
43#endif
44
45const vector<const char*> validationLayers = {
46 "VK_LAYER_KHRONOS_validation"
47};
48
49const vector<const char*> deviceExtensions = {
50 VK_KHR_SWAPCHAIN_EXTENSION_NAME
51};
52
53struct QueueFamilyIndices {
54 optional<uint32_t> graphicsFamily;
55 optional<uint32_t> presentFamily;
56
57 bool isComplete() {
58 return graphicsFamily.has_value() && presentFamily.has_value();
59 }
60};
61
62struct SwapChainSupportDetails {
63 VkSurfaceCapabilitiesKHR capabilities;
64 vector<VkSurfaceFormatKHR> formats;
65 vector<VkPresentModeKHR> presentModes;
66};
67
68struct Vertex {
69 glm::vec3 pos;
70 glm::vec3 color;
71 glm::vec2 texCoord;
72
73 static VkVertexInputBindingDescription getBindingDescription() {
74 VkVertexInputBindingDescription bindingDescription = {};
75
76 bindingDescription.binding = 0;
77 bindingDescription.stride = sizeof(Vertex);
78 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
79
80 return bindingDescription;
81 }
82
83 static array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
84 array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {};
85
86 attributeDescriptions[0].binding = 0;
87 attributeDescriptions[0].location = 0;
88 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
89 attributeDescriptions[0].offset = offsetof(Vertex, pos);
90
91 attributeDescriptions[1].binding = 0;
92 attributeDescriptions[1].location = 1;
93 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
94 attributeDescriptions[1].offset = offsetof(Vertex, color);
95
96 attributeDescriptions[2].binding = 0;
97 attributeDescriptions[2].location = 2;
98 attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
99 attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
100
101 return attributeDescriptions;
102 }
103};
104
105struct UniformBufferObject {
106 alignas(16) glm::mat4 model;
107 alignas(16) glm::mat4 view;
108 alignas(16) glm::mat4 proj;
109};
110
111const vector<Vertex> vertices = {
112 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
113 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
114 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
115 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
116
117 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
118 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
119 {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
120 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
121
122 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
123 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
124 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
125 {{-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
126};
127
128const vector<uint16_t> indices = {
129 0, 1, 2, 2, 3, 0,
130 4, 5, 6, 6, 7, 4,
131 8, 9, 10, 10, 11, 8
132};
133
134VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
135 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
136 const VkAllocationCallbacks* pAllocator,
137 VkDebugUtilsMessengerEXT* pDebugMessenger) {
138 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
139
140 if (func != nullptr) {
141 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
142 } else {
143 return VK_ERROR_EXTENSION_NOT_PRESENT;
144 }
145}
146
147void DestroyDebugUtilsMessengerEXT(VkInstance instance,
148 VkDebugUtilsMessengerEXT debugMessenger,
149 const VkAllocationCallbacks* pAllocator) {
150 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
151
152 if (func != nullptr) {
153 func(instance, debugMessenger, pAllocator);
154 }
155}
156
157class VulkanGame {
158 public:
159 void run() {
160 if (initWindow() == RTWO_ERROR) {
161 return;
162 }
163 initVulkan();
164 mainLoop();
165 cleanup();
166 }
167
168 private:
169 GameGui* gui = new GameGui_SDL();
170 SDL_Window* window = nullptr;
171
172 VkInstance instance;
173 VkDebugUtilsMessengerEXT debugMessenger;
174 VkSurfaceKHR surface;
175 SDL_Surface* sdlSurface = nullptr;
176
177 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
178 VkDevice device;
179
180 VkQueue graphicsQueue;
181 VkQueue presentQueue;
182
183 VkSwapchainKHR swapChain;
184 vector<VkImage> swapChainImages;
185 VkFormat swapChainImageFormat;
186 VkExtent2D swapChainExtent;
187 vector<VkImageView> swapChainImageViews;
188 vector<VkFramebuffer> swapChainFramebuffers;
189
190 VkRenderPass renderPass;
191 VkDescriptorSetLayout descriptorSetLayout;
192 VkPipelineLayout pipelineLayout;
193 VkPipeline graphicsPipeline;
194 VkDescriptorPool descriptorPool;
195 vector<VkDescriptorSet> descriptorSets;
196
197 VkCommandPool commandPool;
198
199 VkImage depthImage;
200 VkDeviceMemory depthImageMemory;
201 VkImageView depthImageView;
202
203 VkImage textureImage;
204 VkDeviceMemory textureImageMemory;
205 VkImageView textureImageView;
206
207 VkImage overlayImage;
208 VkDeviceMemory overlayImageMemory;
209 VkImageView overlayImageView;
210
211 VkSampler textureSampler;
212
213 VkBuffer vertexBuffer;
214 VkDeviceMemory vertexBufferMemory;
215
216 VkBuffer indexBuffer;
217 VkDeviceMemory indexBufferMemory;
218
219 vector<VkBuffer> uniformBuffers;
220 vector<VkDeviceMemory> uniformBuffersMemory;
221
222 vector<VkCommandBuffer> commandBuffers;
223
224 vector<VkSemaphore> imageAvailableSemaphores;
225 vector<VkSemaphore> renderFinishedSemaphores;
226 vector<VkFence> inFlightFences;
227
228 size_t currentFrame = 0;
229
230 bool framebufferResized = false;
231
232 bool initWindow() {
233 if (gui->Init() == RTWO_ERROR) {
234 cout << "UI library could not be initialized!" << endl;
235 return RTWO_ERROR;
236 } else {
237 window = (SDL_Window*) gui->CreateWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT);
238
239 if (window == nullptr) {
240 cout << "Window could not be created!" << endl;
241 return RTWO_ERROR;
242 } else {
243 return RTWO_SUCCESS;
244 }
245 }
246 }
247
248 void initVulkan() {
249 createInstance();
250 setupDebugMessenger();
251 createSurface();
252 pickPhysicalDevice();
253 createLogicalDevice();
254 createSwapChain();
255 createImageViews();
256 createRenderPass();
257 createDescriptorSetLayout();
258 createGraphicsPipeline();
259 createCommandPool();
260 createDepthResources();
261 createFramebuffers();
262 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
263 createImageResources("textures/space.jpg", overlayImage, overlayImageMemory, overlayImageView);
264 createTextureSampler();
265 createVertexBuffer();
266 createIndexBuffer();
267 createUniformBuffers();
268 createDescriptorPool();
269 createDescriptorSets();
270 createCommandBuffers();
271 createSyncObjects();
272 }
273
274 void createInstance() {
275 if (enableValidationLayers && !checkValidationLayerSupport()) {
276 throw runtime_error("validation layers requested, but not available!");
277 }
278
279 VkApplicationInfo appInfo = {};
280 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
281 appInfo.pApplicationName = "Vulkan Game";
282 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
283 appInfo.pEngineName = "No Engine";
284 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
285 appInfo.apiVersion = VK_API_VERSION_1_0;
286
287 VkInstanceCreateInfo createInfo = {};
288 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
289 createInfo.pApplicationInfo = &appInfo;
290
291 vector<const char*> extensions = getRequiredExtensions();
292 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
293 createInfo.ppEnabledExtensionNames = extensions.data();
294
295 cout << endl << "Extensions:" << endl;
296 for (const char* extensionName : extensions) {
297 cout << extensionName << endl;
298 }
299 cout << endl;
300
301 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
302 if (enableValidationLayers) {
303 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
304 createInfo.ppEnabledLayerNames = validationLayers.data();
305
306 populateDebugMessengerCreateInfo(debugCreateInfo);
307 createInfo.pNext = &debugCreateInfo;
308 } else {
309 createInfo.enabledLayerCount = 0;
310
311 createInfo.pNext = nullptr;
312 }
313
314 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
315 throw runtime_error("failed to create instance!");
316 }
317 }
318
319 bool checkValidationLayerSupport() {
320 uint32_t layerCount;
321 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
322
323 vector<VkLayerProperties> availableLayers(layerCount);
324 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
325
326 for (const char* layerName : validationLayers) {
327 bool layerFound = false;
328
329 for (const auto& layerProperties : availableLayers) {
330 if (strcmp(layerName, layerProperties.layerName) == 0) {
331 layerFound = true;
332 break;
333 }
334 }
335
336 if (!layerFound) {
337 return false;
338 }
339 }
340
341 return true;
342 }
343
344 vector<const char*> getRequiredExtensions() {
345 vector<const char*> extensions = gui->GetRequiredExtensions();
346
347 if (enableValidationLayers) {
348 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
349 }
350
351 return extensions;
352 }
353
354 void setupDebugMessenger() {
355 if (!enableValidationLayers) return;
356
357 VkDebugUtilsMessengerCreateInfoEXT createInfo;
358 populateDebugMessengerCreateInfo(createInfo);
359
360 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
361 throw runtime_error("failed to set up debug messenger!");
362 }
363 }
364
365 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
366 createInfo = {};
367 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
368 createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
369 createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
370 createInfo.pfnUserCallback = debugCallback;
371 }
372
373 void createSurface() {
374 sdlSurface = SDL_GetWindowSurface(window);
375
376 if (sdlSurface == nullptr) {
377 cout << "Could not get SDL Surface! =(" << endl;
378 }
379
380 if (gui->CreateVulkanSurface(instance, &surface) == RTWO_ERROR) {
381 throw runtime_error("failed to create window surface!");
382 }
383 }
384
385 void pickPhysicalDevice() {
386 uint32_t deviceCount = 0;
387 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
388
389 if (deviceCount == 0) {
390 throw runtime_error("failed to find GPUs with Vulkan support!");
391 }
392
393 vector<VkPhysicalDevice> devices(deviceCount);
394 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
395
396 cout << endl << "Graphics cards:" << endl;
397 for (const VkPhysicalDevice& device : devices) {
398 if (isDeviceSuitable(device)) {
399 physicalDevice = device;
400 break;
401 }
402 }
403 cout << endl;
404
405 if (physicalDevice == VK_NULL_HANDLE) {
406 throw runtime_error("failed to find a suitable GPU!");
407 }
408 }
409
410 bool isDeviceSuitable(VkPhysicalDevice device) {
411 VkPhysicalDeviceProperties deviceProperties;
412 vkGetPhysicalDeviceProperties(device, &deviceProperties);
413
414 cout << "Device: " << deviceProperties.deviceName << endl;
415
416 QueueFamilyIndices indices = findQueueFamilies(device);
417 bool extensionsSupported = checkDeviceExtensionSupport(device);
418 bool swapChainAdequate = false;
419
420 if (extensionsSupported) {
421 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
422 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
423 }
424
425 VkPhysicalDeviceFeatures supportedFeatures;
426 vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
427
428 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
429 }
430
431 bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
432 uint32_t extensionCount;
433 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
434
435 vector<VkExtensionProperties> availableExtensions(extensionCount);
436 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
437
438 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
439
440 for (const auto& extension : availableExtensions) {
441 requiredExtensions.erase(extension.extensionName);
442 }
443
444 return requiredExtensions.empty();
445 }
446
447 void createLogicalDevice() {
448 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
449
450 vector<VkDeviceQueueCreateInfo> queueCreateInfos;
451 set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
452
453 float queuePriority = 1.0f;
454 for (uint32_t queueFamily : uniqueQueueFamilies) {
455 VkDeviceQueueCreateInfo queueCreateInfo = {};
456 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
457 queueCreateInfo.queueFamilyIndex = queueFamily;
458 queueCreateInfo.queueCount = 1;
459 queueCreateInfo.pQueuePriorities = &queuePriority;
460
461 queueCreateInfos.push_back(queueCreateInfo);
462 }
463
464 VkPhysicalDeviceFeatures deviceFeatures = {};
465 deviceFeatures.samplerAnisotropy = VK_TRUE;
466
467 VkDeviceCreateInfo createInfo = {};
468 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
469 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
470 createInfo.pQueueCreateInfos = queueCreateInfos.data();
471
472 createInfo.pEnabledFeatures = &deviceFeatures;
473
474 createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
475 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
476
477 // These fields are ignored by up-to-date Vulkan implementations,
478 // but it's a good idea to set them for backwards compatibility
479 if (enableValidationLayers) {
480 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
481 createInfo.ppEnabledLayerNames = validationLayers.data();
482 } else {
483 createInfo.enabledLayerCount = 0;
484 }
485
486 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
487 throw runtime_error("failed to create logical device!");
488 }
489
490 vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
491 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
492 }
493
494 void createSwapChain() {
495 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
496
497 VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
498 VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
499 VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
500
501 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
502 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
503 imageCount = swapChainSupport.capabilities.maxImageCount;
504 }
505
506 VkSwapchainCreateInfoKHR createInfo = {};
507 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
508 createInfo.surface = surface;
509 createInfo.minImageCount = imageCount;
510 createInfo.imageFormat = surfaceFormat.format;
511 createInfo.imageColorSpace = surfaceFormat.colorSpace;
512 createInfo.imageExtent = extent;
513 createInfo.imageArrayLayers = 1;
514 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
515
516 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
517 uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
518
519 if (indices.graphicsFamily != indices.presentFamily) {
520 createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
521 createInfo.queueFamilyIndexCount = 2;
522 createInfo.pQueueFamilyIndices = queueFamilyIndices;
523 } else {
524 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
525 createInfo.queueFamilyIndexCount = 0;
526 createInfo.pQueueFamilyIndices = nullptr;
527 }
528
529 createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
530 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
531 createInfo.presentMode = presentMode;
532 createInfo.clipped = VK_TRUE;
533 createInfo.oldSwapchain = VK_NULL_HANDLE;
534
535 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
536 throw runtime_error("failed to create swap chain!");
537 }
538
539 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
540 swapChainImages.resize(imageCount);
541 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
542
543 swapChainImageFormat = surfaceFormat.format;
544 swapChainExtent = extent;
545 }
546
547 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
548 SwapChainSupportDetails details;
549
550 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
551
552 uint32_t formatCount;
553 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
554
555 if (formatCount != 0) {
556 details.formats.resize(formatCount);
557 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
558 }
559
560 uint32_t presentModeCount;
561 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
562
563 if (presentModeCount != 0) {
564 details.presentModes.resize(presentModeCount);
565 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
566 }
567
568 return details;
569 }
570
571 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
572 for (const auto& availableFormat : availableFormats) {
573 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
574 return availableFormat;
575 }
576 }
577
578 return availableFormats[0];
579 }
580
581 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
582 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
583
584 for (const auto& availablePresentMode : availablePresentModes) {
585 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
586 return availablePresentMode;
587 }
588 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
589 bestMode = availablePresentMode;
590 }
591 }
592
593 return bestMode;
594 }
595
596 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
597 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
598 return capabilities.currentExtent;
599 }
600 else {
601 int width, height;
602 gui->GetWindowSize(&width, &height);
603
604 VkExtent2D actualExtent = {
605 static_cast<uint32_t>(width),
606 static_cast<uint32_t>(height)
607 };
608
609 actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width));
610 actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height));
611
612 return actualExtent;
613 }
614 }
615
616 void createImageViews() {
617 swapChainImageViews.resize(swapChainImages.size());
618
619 for (size_t i = 0; i < swapChainImages.size(); i++) {
620 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
621 }
622 }
623
624 void createRenderPass() {
625 VkAttachmentDescription colorAttachment = {};
626 colorAttachment.format = swapChainImageFormat;
627 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
628 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
629 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
630 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
631 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
632 colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
633 colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
634
635 VkAttachmentReference colorAttachmentRef = {};
636 colorAttachmentRef.attachment = 0;
637 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
638
639 VkAttachmentDescription depthAttachment = {};
640 depthAttachment.format = findDepthFormat();
641 depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
642 depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
643 depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
644 depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
645 depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
646 depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
647 depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
648
649 VkAttachmentReference depthAttachmentRef = {};
650 depthAttachmentRef.attachment = 1;
651 depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
652
653 VkSubpassDescription subpass = {};
654 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
655 subpass.colorAttachmentCount = 1;
656 subpass.pColorAttachments = &colorAttachmentRef;
657 subpass.pDepthStencilAttachment = &depthAttachmentRef;
658
659 VkSubpassDependency dependency = {};
660 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
661 dependency.dstSubpass = 0;
662 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
663 dependency.srcAccessMask = 0;
664 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
665 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
666
667 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
668 VkRenderPassCreateInfo renderPassInfo = {};
669 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
670 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
671 renderPassInfo.pAttachments = attachments.data();
672 renderPassInfo.subpassCount = 1;
673 renderPassInfo.pSubpasses = &subpass;
674 renderPassInfo.dependencyCount = 1;
675 renderPassInfo.pDependencies = &dependency;
676
677 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
678 throw runtime_error("failed to create render pass!");
679 }
680 }
681
682 void createDescriptorSetLayout() {
683 VkDescriptorSetLayoutBinding uboLayoutBinding = {};
684 uboLayoutBinding.binding = 0;
685 uboLayoutBinding.descriptorCount = 1;
686 uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
687 uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
688 uboLayoutBinding.pImmutableSamplers = nullptr;
689
690 VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
691 samplerLayoutBinding.binding = 1;
692 samplerLayoutBinding.descriptorCount = 1;
693 samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
694 samplerLayoutBinding.pImmutableSamplers = nullptr;
695 samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
696
697 VkDescriptorSetLayoutBinding overlaySamplerLayoutBinding = {};
698 overlaySamplerLayoutBinding.binding = 2;
699 overlaySamplerLayoutBinding.descriptorCount = 1;
700 overlaySamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
701 overlaySamplerLayoutBinding.pImmutableSamplers = nullptr;
702 overlaySamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
703
704 array<VkDescriptorSetLayoutBinding, 3> bindings = { uboLayoutBinding, samplerLayoutBinding, overlaySamplerLayoutBinding };
705 VkDescriptorSetLayoutCreateInfo layoutInfo = {};
706 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
707 layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
708 layoutInfo.pBindings = bindings.data();
709
710 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
711 throw runtime_error("failed to create descriptor set layout!");
712 }
713 }
714
715 void createGraphicsPipeline() {
716 auto vertShaderCode = readFile("shaders/vert.spv");
717 auto fragShaderCode = readFile("shaders/frag.spv");
718
719 VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
720 VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
721
722 VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
723 vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
724 vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
725 vertShaderStageInfo.module = vertShaderModule;
726 vertShaderStageInfo.pName = "main";
727
728 VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
729 fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
730 fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
731 fragShaderStageInfo.module = fragShaderModule;
732 fragShaderStageInfo.pName = "main";
733
734 VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
735
736 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
737 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
738
739 auto bindingDescription = Vertex::getBindingDescription();
740 auto attributeDescriptions = Vertex::getAttributeDescriptions();
741
742 vertexInputInfo.vertexBindingDescriptionCount = 1;
743 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
744 vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
745 vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
746
747 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
748 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
749 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
750 inputAssembly.primitiveRestartEnable = VK_FALSE;
751
752 VkViewport viewport = {};
753 viewport.x = 0.0f;
754 viewport.y = 0.0f;
755 viewport.width = (float) swapChainExtent.width;
756 viewport.height = (float) swapChainExtent.height;
757 viewport.minDepth = 0.0f;
758 viewport.maxDepth = 1.0f;
759
760 VkRect2D scissor = {};
761 scissor.offset = { 0, 0 };
762 scissor.extent = swapChainExtent;
763
764 VkPipelineViewportStateCreateInfo viewportState = {};
765 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
766 viewportState.viewportCount = 1;
767 viewportState.pViewports = &viewport;
768 viewportState.scissorCount = 1;
769 viewportState.pScissors = &scissor;
770
771 VkPipelineRasterizationStateCreateInfo rasterizer = {};
772 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
773 rasterizer.depthClampEnable = VK_FALSE;
774 rasterizer.rasterizerDiscardEnable = VK_FALSE;
775 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
776 rasterizer.lineWidth = 1.0f;
777 rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
778 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
779 rasterizer.depthBiasEnable = VK_FALSE;
780
781 VkPipelineMultisampleStateCreateInfo multisampling = {};
782 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
783 multisampling.sampleShadingEnable = VK_FALSE;
784 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
785
786 VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
787 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
788 colorBlendAttachment.blendEnable = VK_TRUE;
789 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
790 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
791 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
792 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
793 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
794 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
795
796 VkPipelineColorBlendStateCreateInfo colorBlending = {};
797 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
798 colorBlending.logicOpEnable = VK_FALSE;
799 colorBlending.logicOp = VK_LOGIC_OP_COPY;
800 colorBlending.attachmentCount = 1;
801 colorBlending.pAttachments = &colorBlendAttachment;
802 colorBlending.blendConstants[0] = 0.0f;
803 colorBlending.blendConstants[1] = 0.0f;
804 colorBlending.blendConstants[2] = 0.0f;
805 colorBlending.blendConstants[3] = 0.0f;
806
807 VkPipelineDepthStencilStateCreateInfo depthStencil = {};
808 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
809 depthStencil.depthTestEnable = VK_TRUE;
810 depthStencil.depthWriteEnable = VK_TRUE;
811 depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
812 depthStencil.depthBoundsTestEnable = VK_FALSE;
813 depthStencil.minDepthBounds = 0.0f;
814 depthStencil.maxDepthBounds = 1.0f;
815 depthStencil.stencilTestEnable = VK_FALSE;
816 depthStencil.front = {};
817 depthStencil.back = {};
818
819 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
820 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
821 pipelineLayoutInfo.setLayoutCount = 1;
822 pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
823 pipelineLayoutInfo.pushConstantRangeCount = 0;
824
825 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
826 throw runtime_error("failed to create pipeline layout!");
827 }
828
829 VkGraphicsPipelineCreateInfo pipelineInfo = {};
830 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
831 pipelineInfo.stageCount = 2;
832 pipelineInfo.pStages = shaderStages;
833 pipelineInfo.pVertexInputState = &vertexInputInfo;
834 pipelineInfo.pInputAssemblyState = &inputAssembly;
835 pipelineInfo.pViewportState = &viewportState;
836 pipelineInfo.pRasterizationState = &rasterizer;
837 pipelineInfo.pMultisampleState = &multisampling;
838 pipelineInfo.pDepthStencilState = &depthStencil;
839 pipelineInfo.pColorBlendState = &colorBlending;
840 pipelineInfo.pDynamicState = nullptr;
841 pipelineInfo.layout = pipelineLayout;
842 pipelineInfo.renderPass = renderPass;
843 pipelineInfo.subpass = 0;
844 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
845 pipelineInfo.basePipelineIndex = -1;
846
847 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
848 throw runtime_error("failed to create graphics pipeline!");
849 }
850
851 vkDestroyShaderModule(device, vertShaderModule, nullptr);
852 vkDestroyShaderModule(device, fragShaderModule, nullptr);
853 }
854
855 VkShaderModule createShaderModule(const vector<char>& code) {
856 VkShaderModuleCreateInfo createInfo = {};
857 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
858 createInfo.codeSize = code.size();
859 createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
860
861 VkShaderModule shaderModule;
862 if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
863 throw runtime_error("failed to create shader module!");
864 }
865
866 return shaderModule;
867 }
868
869 void createFramebuffers() {
870 swapChainFramebuffers.resize(swapChainImageViews.size());
871
872 for (size_t i = 0; i < swapChainImageViews.size(); i++) {
873 array <VkImageView, 2> attachments = {
874 swapChainImageViews[i],
875 depthImageView
876 };
877
878 VkFramebufferCreateInfo framebufferInfo = {};
879 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
880 framebufferInfo.renderPass = renderPass;
881 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
882 framebufferInfo.pAttachments = attachments.data();
883 framebufferInfo.width = swapChainExtent.width;
884 framebufferInfo.height = swapChainExtent.height;
885 framebufferInfo.layers = 1;
886
887 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
888 throw runtime_error("failed to create framebuffer!");
889 }
890 }
891 }
892
893 void createCommandPool() {
894 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
895
896 VkCommandPoolCreateInfo poolInfo = {};
897 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
898 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
899 poolInfo.flags = 0;
900
901 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
902 throw runtime_error("failed to create graphics command pool!");
903 }
904 }
905
906 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
907 QueueFamilyIndices indices;
908
909 uint32_t queueFamilyCount = 0;
910 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
911
912 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
913 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
914
915 int i = 0;
916 for (const auto& queueFamily : queueFamilies) {
917 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
918 indices.graphicsFamily = i;
919 }
920
921 VkBool32 presentSupport = false;
922 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
923
924 if (queueFamily.queueCount > 0 && presentSupport) {
925 indices.presentFamily = i;
926 }
927
928 if (indices.isComplete()) {
929 break;
930 }
931
932 i++;
933 }
934
935 return indices;
936 }
937
938 void createDepthResources() {
939 VkFormat depthFormat = findDepthFormat();
940
941 createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
942 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
943 depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
944
945 transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
946 }
947
948 VkFormat findDepthFormat() {
949 return findSupportedFormat(
950 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
951 VK_IMAGE_TILING_OPTIMAL,
952 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
953 );
954 }
955
956 VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling,
957 VkFormatFeatureFlags features) {
958 for (VkFormat format : candidates) {
959 VkFormatProperties props;
960 vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
961
962 if (tiling == VK_IMAGE_TILING_LINEAR &&
963 (props.linearTilingFeatures & features) == features) {
964 return format;
965 } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
966 (props.optimalTilingFeatures & features) == features) {
967 return format;
968 }
969 }
970
971 throw runtime_error("failed to find supported format!");
972 }
973
974 bool hasStencilComponent(VkFormat format) {
975 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
976 }
977
978 void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
979 int texWidth, texHeight, texChannels;
980
981 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
982 VkDeviceSize imageSize = texWidth * texHeight * 4;
983
984 if (!pixels) {
985 throw runtime_error("failed to load texture image!");
986 }
987
988 VkBuffer stagingBuffer;
989 VkDeviceMemory stagingBufferMemory;
990
991 createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
992 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
993 stagingBuffer, stagingBufferMemory);
994
995 void* data;
996
997 vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
998 memcpy(data, pixels, static_cast<size_t>(imageSize));
999 vkUnmapMemory(device, stagingBufferMemory);
1000
1001 stbi_image_free(pixels);
1002
1003 createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1004 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1005 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
1006
1007 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
1008 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
1009 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1010
1011 vkDestroyBuffer(device, stagingBuffer, nullptr);
1012 vkFreeMemory(device, stagingBufferMemory, nullptr);
1013
1014 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
1015 }
1016
1017 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
1018 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
1019 VkImageCreateInfo imageInfo = {};
1020 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1021 imageInfo.imageType = VK_IMAGE_TYPE_2D;
1022 imageInfo.extent.width = width;
1023 imageInfo.extent.height = height;
1024 imageInfo.extent.depth = 1;
1025 imageInfo.mipLevels = 1;
1026 imageInfo.arrayLayers = 1;
1027 imageInfo.format = format;
1028 imageInfo.tiling = tiling;
1029 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1030 imageInfo.usage = usage;
1031 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
1032 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1033
1034 if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
1035 throw runtime_error("failed to create image!");
1036 }
1037
1038 VkMemoryRequirements memRequirements;
1039 vkGetImageMemoryRequirements(device, image, &memRequirements);
1040
1041 VkMemoryAllocateInfo allocInfo = {};
1042 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1043 allocInfo.allocationSize = memRequirements.size;
1044 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1045
1046 if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
1047 throw runtime_error("failed to allocate image memory!");
1048 }
1049
1050 vkBindImageMemory(device, image, imageMemory, 0);
1051 }
1052
1053 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
1054 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1055
1056 VkImageMemoryBarrier barrier = {};
1057 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1058 barrier.oldLayout = oldLayout;
1059 barrier.newLayout = newLayout;
1060 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1061 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1062 barrier.image = image;
1063
1064 if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1065 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1066
1067 if (hasStencilComponent(format)) {
1068 barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
1069 }
1070 } else {
1071 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1072 }
1073
1074 barrier.subresourceRange.baseMipLevel = 0;
1075 barrier.subresourceRange.levelCount = 1;
1076 barrier.subresourceRange.baseArrayLayer = 0;
1077 barrier.subresourceRange.layerCount = 1;
1078
1079 VkPipelineStageFlags sourceStage;
1080 VkPipelineStageFlags destinationStage;
1081
1082 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1083 barrier.srcAccessMask = 0;
1084 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1085
1086 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1087 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1088 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1089 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1090 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1091
1092 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1093 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
1094 } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1095 barrier.srcAccessMask = 0;
1096 barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
1097
1098 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1099 destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
1100 } else {
1101 throw invalid_argument("unsupported layout transition!");
1102 }
1103
1104 vkCmdPipelineBarrier(
1105 commandBuffer,
1106 sourceStage, destinationStage,
1107 0,
1108 0, nullptr,
1109 0, nullptr,
1110 1, &barrier
1111 );
1112
1113 endSingleTimeCommands(commandBuffer);
1114 }
1115
1116 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
1117 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1118
1119 VkBufferImageCopy region = {};
1120 region.bufferOffset = 0;
1121 region.bufferRowLength = 0;
1122 region.bufferImageHeight = 0;
1123 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1124 region.imageSubresource.mipLevel = 0;
1125 region.imageSubresource.baseArrayLayer = 0;
1126 region.imageSubresource.layerCount = 1;
1127 region.imageOffset = { 0, 0, 0 };
1128 region.imageExtent = { width, height, 1 };
1129
1130 vkCmdCopyBufferToImage(
1131 commandBuffer,
1132 buffer,
1133 image,
1134 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1135 1,
1136 &region
1137 );
1138
1139 endSingleTimeCommands(commandBuffer);
1140 }
1141
1142 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
1143 VkImageViewCreateInfo viewInfo = {};
1144 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1145 viewInfo.image = image;
1146 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
1147 viewInfo.format = format;
1148
1149 viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
1150 viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
1151 viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
1152 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
1153
1154 viewInfo.subresourceRange.aspectMask = aspectFlags;
1155 viewInfo.subresourceRange.baseMipLevel = 0;
1156 viewInfo.subresourceRange.levelCount = 1;
1157 viewInfo.subresourceRange.baseArrayLayer = 0;
1158 viewInfo.subresourceRange.layerCount = 1;
1159
1160 VkImageView imageView;
1161 if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
1162 throw runtime_error("failed to create texture image view!");
1163 }
1164
1165 return imageView;
1166 }
1167
1168 void createTextureSampler() {
1169 VkSamplerCreateInfo samplerInfo = {};
1170 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1171 samplerInfo.magFilter = VK_FILTER_LINEAR;
1172 samplerInfo.minFilter = VK_FILTER_LINEAR;
1173
1174 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1175 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1176 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1177
1178 samplerInfo.anisotropyEnable = VK_TRUE;
1179 samplerInfo.maxAnisotropy = 16;
1180 samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
1181 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1182 samplerInfo.compareEnable = VK_FALSE;
1183 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
1184 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
1185 samplerInfo.mipLodBias = 0.0f;
1186 samplerInfo.minLod = 0.0f;
1187 samplerInfo.maxLod = 0.0f;
1188
1189 if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
1190 throw runtime_error("failed to create texture sampler!");
1191 }
1192 }
1193
1194 void createVertexBuffer() {
1195 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
1196
1197 VkBuffer stagingBuffer;
1198 VkDeviceMemory stagingBufferMemory;
1199 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1200 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1201 stagingBuffer, stagingBufferMemory);
1202
1203 void* data;
1204 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1205 memcpy(data, vertices.data(), (size_t) bufferSize);
1206 vkUnmapMemory(device, stagingBufferMemory);
1207
1208 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
1209 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
1210
1211 copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
1212
1213 vkDestroyBuffer(device, stagingBuffer, nullptr);
1214 vkFreeMemory(device, stagingBufferMemory, nullptr);
1215 }
1216
1217 void createIndexBuffer() {
1218 VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();
1219
1220 VkBuffer stagingBuffer;
1221 VkDeviceMemory stagingBufferMemory;
1222 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1223 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1224 stagingBuffer, stagingBufferMemory);
1225
1226 void* data;
1227 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1228 memcpy(data, indices.data(), (size_t) bufferSize);
1229 vkUnmapMemory(device, stagingBufferMemory);
1230
1231 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
1232 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
1233
1234 copyBuffer(stagingBuffer, indexBuffer, bufferSize);
1235
1236 vkDestroyBuffer(device, stagingBuffer, nullptr);
1237 vkFreeMemory(device, stagingBufferMemory, nullptr);
1238 }
1239
1240 void createUniformBuffers() {
1241 VkDeviceSize bufferSize = sizeof(UniformBufferObject);
1242
1243 uniformBuffers.resize(swapChainImages.size());
1244 uniformBuffersMemory.resize(swapChainImages.size());
1245
1246 for (size_t i = 0; i < swapChainImages.size(); i++) {
1247 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1248 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1249 uniformBuffers[i], uniformBuffersMemory[i]);
1250 }
1251 }
1252
1253 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
1254 VkBufferCreateInfo bufferInfo = {};
1255 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1256 bufferInfo.size = size;
1257 bufferInfo.usage = usage;
1258 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1259
1260 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1261 throw runtime_error("failed to create buffer!");
1262 }
1263
1264 VkMemoryRequirements memRequirements;
1265 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
1266
1267 VkMemoryAllocateInfo allocInfo = {};
1268 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1269 allocInfo.allocationSize = memRequirements.size;
1270 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1271
1272 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1273 throw runtime_error("failed to allocate buffer memory!");
1274 }
1275
1276 vkBindBufferMemory(device, buffer, bufferMemory, 0);
1277 }
1278
1279 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
1280 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1281
1282 VkBufferCopy copyRegion = {};
1283 copyRegion.size = size;
1284 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
1285
1286 endSingleTimeCommands(commandBuffer);
1287 }
1288
1289 VkCommandBuffer beginSingleTimeCommands() {
1290 VkCommandBufferAllocateInfo allocInfo = {};
1291 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1292 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1293 allocInfo.commandPool = commandPool;
1294 allocInfo.commandBufferCount = 1;
1295
1296 VkCommandBuffer commandBuffer;
1297 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
1298
1299 VkCommandBufferBeginInfo beginInfo = {};
1300 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1301 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1302
1303 vkBeginCommandBuffer(commandBuffer, &beginInfo);
1304
1305 return commandBuffer;
1306 }
1307
1308 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
1309 vkEndCommandBuffer(commandBuffer);
1310
1311 VkSubmitInfo submitInfo = {};
1312 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1313 submitInfo.commandBufferCount = 1;
1314 submitInfo.pCommandBuffers = &commandBuffer;
1315
1316 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
1317 vkQueueWaitIdle(graphicsQueue);
1318
1319 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
1320 }
1321
1322 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
1323 VkPhysicalDeviceMemoryProperties memProperties;
1324 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
1325
1326 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
1327 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
1328 return i;
1329 }
1330 }
1331
1332 throw runtime_error("failed to find suitable memory type!");
1333 }
1334
1335 void createDescriptorPool() {
1336 array<VkDescriptorPoolSize, 3> poolSizes = {};
1337 poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1338 poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
1339 poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1340 poolSizes[1].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
1341 poolSizes[2].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1342 poolSizes[2].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
1343
1344 VkDescriptorPoolCreateInfo poolInfo = {};
1345 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1346 poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
1347 poolInfo.pPoolSizes = poolSizes.data();
1348 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
1349
1350 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
1351 throw runtime_error("failed to create descriptor pool!");
1352 }
1353 }
1354
1355 void createDescriptorSets() {
1356 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
1357
1358 VkDescriptorSetAllocateInfo allocInfo = {};
1359 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1360 allocInfo.descriptorPool = descriptorPool;
1361 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
1362 allocInfo.pSetLayouts = layouts.data();
1363
1364 descriptorSets.resize(swapChainImages.size());
1365 if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
1366 throw runtime_error("failed to allocate descriptor sets!");
1367 }
1368
1369 for (size_t i = 0; i < swapChainImages.size(); i++) {
1370 VkDescriptorBufferInfo bufferInfo = {};
1371 bufferInfo.buffer = uniformBuffers[i];
1372 bufferInfo.offset = 0;
1373 bufferInfo.range = sizeof(UniformBufferObject);
1374
1375 VkDescriptorImageInfo imageInfo = {};
1376 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1377 imageInfo.imageView = textureImageView;
1378 imageInfo.sampler = textureSampler;
1379
1380 VkDescriptorImageInfo overlayImageInfo = {};
1381 overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1382 overlayImageInfo.imageView = overlayImageView;
1383 overlayImageInfo.sampler = textureSampler;
1384
1385 array<VkWriteDescriptorSet, 3> descriptorWrites = {};
1386
1387 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1388 descriptorWrites[0].dstSet = descriptorSets[i];
1389 descriptorWrites[0].dstBinding = 0;
1390 descriptorWrites[0].dstArrayElement = 0;
1391 descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1392 descriptorWrites[0].descriptorCount = 1;
1393 descriptorWrites[0].pBufferInfo = &bufferInfo;
1394 descriptorWrites[0].pImageInfo = nullptr;
1395 descriptorWrites[0].pTexelBufferView = nullptr;
1396
1397 descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1398 descriptorWrites[1].dstSet = descriptorSets[i];
1399 descriptorWrites[1].dstBinding = 1;
1400 descriptorWrites[1].dstArrayElement = 0;
1401 descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1402 descriptorWrites[1].descriptorCount = 1;
1403 descriptorWrites[1].pBufferInfo = nullptr;
1404 descriptorWrites[1].pImageInfo = &imageInfo;
1405 descriptorWrites[1].pTexelBufferView = nullptr;
1406
1407 descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1408 descriptorWrites[2].dstSet = descriptorSets[i];
1409 descriptorWrites[2].dstBinding = 2;
1410 descriptorWrites[2].dstArrayElement = 0;
1411 descriptorWrites[2].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1412 descriptorWrites[2].descriptorCount = 1;
1413 descriptorWrites[2].pBufferInfo = nullptr;
1414 descriptorWrites[2].pImageInfo = &overlayImageInfo;
1415 descriptorWrites[2].pTexelBufferView = nullptr;
1416
1417 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
1418 }
1419 }
1420
1421 void createCommandBuffers() {
1422 commandBuffers.resize(swapChainFramebuffers.size());
1423
1424 VkCommandBufferAllocateInfo allocInfo = {};
1425 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1426 allocInfo.commandPool = commandPool;
1427 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1428 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
1429
1430 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
1431 throw runtime_error("failed to allocate command buffers!");
1432 }
1433
1434 for (size_t i = 0; i < commandBuffers.size(); i++) {
1435 VkCommandBufferBeginInfo beginInfo = {};
1436 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1437 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
1438 beginInfo.pInheritanceInfo = nullptr;
1439
1440 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
1441 throw runtime_error("failed to begin recording command buffer!");
1442 }
1443
1444 VkRenderPassBeginInfo renderPassInfo = {};
1445 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1446 renderPassInfo.renderPass = renderPass;
1447 renderPassInfo.framebuffer = swapChainFramebuffers[i];
1448 renderPassInfo.renderArea.offset = { 0, 0 };
1449 renderPassInfo.renderArea.extent = swapChainExtent;
1450
1451 array<VkClearValue, 2> clearValues = {};
1452 clearValues[0].color = { 0.0f, 0.0f, 0.0f, 1.0f };
1453 clearValues[1].depthStencil = { 1.0f, 0 };
1454
1455 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
1456 renderPassInfo.pClearValues = clearValues.data();
1457
1458 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
1459
1460 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
1461
1462 VkBuffer vertexBuffers[] = { vertexBuffer };
1463 VkDeviceSize offsets[] = { 0 };
1464 vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
1465
1466 vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
1467 vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i], 0, nullptr);
1468
1469 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
1470
1471 vkCmdEndRenderPass(commandBuffers[i]);
1472
1473 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
1474 throw runtime_error("failed to record command buffer!");
1475 }
1476 }
1477 }
1478
1479 void createSyncObjects() {
1480 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1481 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1482 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
1483
1484 VkSemaphoreCreateInfo semaphoreInfo = {};
1485 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1486
1487 VkFenceCreateInfo fenceInfo = {};
1488 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1489 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
1490
1491 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1492 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
1493 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
1494 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
1495 throw runtime_error("failed to create synchronization objects for a frame!");
1496 }
1497 }
1498 }
1499
1500 void mainLoop() {
1501 // TODO: Create some generic event-handling functions in game-gui-*
1502 SDL_Event e;
1503 bool quit = false;
1504
1505 while (!quit) {
1506 while (SDL_PollEvent(&e)) {
1507 if (e.type == SDL_QUIT) {
1508 quit = true;
1509 }
1510 if (e.type == SDL_KEYDOWN) {
1511 quit = true;
1512 }
1513 if (e.type == SDL_MOUSEBUTTONDOWN) {
1514 quit = true;
1515 }
1516 if (e.type == SDL_WINDOWEVENT) {
1517 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
1518 framebufferResized = true;
1519 } else if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
1520 framebufferResized = true;
1521 }
1522 }
1523 }
1524
1525 drawFrame();
1526
1527 //SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0x00, 0x99, 0x99));
1528 //SDL_UpdateWindowSurface(window);
1529 }
1530
1531 vkDeviceWaitIdle(device);
1532 }
1533
1534 void drawFrame() {
1535 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
1536
1537 uint32_t imageIndex;
1538
1539 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
1540 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
1541
1542 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
1543 recreateSwapChain();
1544 return;
1545 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
1546 throw runtime_error("failed to acquire swap chain image!");
1547 }
1548
1549 updateUniformBuffer(imageIndex);
1550
1551 VkSubmitInfo submitInfo = {};
1552 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1553
1554 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
1555 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
1556
1557 submitInfo.waitSemaphoreCount = 1;
1558 submitInfo.pWaitSemaphores = waitSemaphores;
1559 submitInfo.pWaitDstStageMask = waitStages;
1560 submitInfo.commandBufferCount = 1;
1561 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
1562
1563 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
1564
1565 submitInfo.signalSemaphoreCount = 1;
1566 submitInfo.pSignalSemaphores = signalSemaphores;
1567
1568 vkResetFences(device, 1, &inFlightFences[currentFrame]);
1569
1570 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
1571 throw runtime_error("failed to submit draw command buffer!");
1572 }
1573
1574 VkPresentInfoKHR presentInfo = {};
1575 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1576 presentInfo.waitSemaphoreCount = 1;
1577 presentInfo.pWaitSemaphores = signalSemaphores;
1578
1579 VkSwapchainKHR swapChains[] = { swapChain };
1580 presentInfo.swapchainCount = 1;
1581 presentInfo.pSwapchains = swapChains;
1582 presentInfo.pImageIndices = &imageIndex;
1583 presentInfo.pResults = nullptr;
1584
1585 result = vkQueuePresentKHR(presentQueue, &presentInfo);
1586
1587 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
1588 framebufferResized = false;
1589 recreateSwapChain();
1590 } else if (result != VK_SUCCESS) {
1591 throw runtime_error("failed to present swap chain image!");
1592 }
1593
1594 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
1595 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
1596 }
1597
1598 void updateUniformBuffer(uint32_t currentImage) {
1599 static auto startTime = chrono::high_resolution_clock::now();
1600
1601 auto currentTime = chrono::high_resolution_clock::now();
1602 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
1603
1604 UniformBufferObject ubo = {};
1605 ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
1606 ubo.view = glm::lookAt(glm::vec3(0.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
1607 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
1608 ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
1609
1610 void* data;
1611 vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
1612 memcpy(data, &ubo, sizeof(ubo));
1613 vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
1614 }
1615
1616 void recreateSwapChain() {
1617 int width = 0, height = 0;
1618
1619 gui->GetWindowSize(&width, &height);
1620
1621 while (width == 0 || height == 0 ||
1622 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
1623 SDL_WaitEvent(nullptr);
1624 gui->GetWindowSize(&width, &height);
1625 }
1626
1627 vkDeviceWaitIdle(device);
1628
1629 cleanupSwapChain();
1630
1631 createSwapChain();
1632 createImageViews();
1633 createRenderPass();
1634 createGraphicsPipeline();
1635 createDepthResources();
1636 createFramebuffers();
1637 createUniformBuffers();
1638 createDescriptorPool();
1639 createDescriptorSets();
1640 createCommandBuffers();
1641 }
1642
1643 void cleanup() {
1644 cleanupSwapChain();
1645
1646 vkDestroySampler(device, textureSampler, nullptr);
1647
1648 vkDestroyImageView(device, textureImageView, nullptr);
1649 vkDestroyImage(device, textureImage, nullptr);
1650 vkFreeMemory(device, textureImageMemory, nullptr);
1651
1652 vkDestroyImageView(device, overlayImageView, nullptr);
1653 vkDestroyImage(device, overlayImage, nullptr);
1654 vkFreeMemory(device, overlayImageMemory, nullptr);
1655
1656 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
1657
1658 vkDestroyBuffer(device, indexBuffer, nullptr);
1659 vkFreeMemory(device, indexBufferMemory, nullptr);
1660
1661 vkDestroyBuffer(device, vertexBuffer, nullptr);
1662 vkFreeMemory(device, vertexBufferMemory, nullptr);
1663
1664 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1665 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
1666 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
1667 vkDestroyFence(device, inFlightFences[i], nullptr);
1668 }
1669
1670 vkDestroyCommandPool(device, commandPool, nullptr);
1671
1672 vkDestroyDevice(device, nullptr);
1673
1674 if (enableValidationLayers) {
1675 DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
1676 }
1677
1678 vkDestroySurfaceKHR(instance, surface, nullptr);
1679 vkDestroyInstance(instance, nullptr);
1680
1681 gui->DestroyWindow();
1682 gui->Shutdown();
1683 delete gui;
1684 }
1685
1686 void cleanupSwapChain() {
1687 vkDestroyImageView(device, depthImageView, nullptr);
1688 vkDestroyImage(device, depthImage, nullptr);
1689 vkFreeMemory(device, depthImageMemory, nullptr);
1690
1691 for (auto framebuffer : swapChainFramebuffers) {
1692 vkDestroyFramebuffer(device, framebuffer, nullptr);
1693 }
1694
1695 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
1696
1697 vkDestroyPipeline(device, graphicsPipeline, nullptr);
1698 vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
1699 vkDestroyRenderPass(device, renderPass, nullptr);
1700
1701 for (auto imageView : swapChainImageViews) {
1702 vkDestroyImageView(device, imageView, nullptr);
1703 }
1704
1705 vkDestroySwapchainKHR(device, swapChain, nullptr);
1706
1707 for (size_t i = 0; i < swapChainImages.size(); i++) {
1708 vkDestroyBuffer(device, uniformBuffers[i], nullptr);
1709 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
1710 }
1711
1712 vkDestroyDescriptorPool(device, descriptorPool, nullptr);
1713 }
1714
1715 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
1716 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
1717 VkDebugUtilsMessageTypeFlagsEXT messageType,
1718 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
1719 void* pUserData) {
1720 cerr << "validation layer: " << pCallbackData->pMessage << endl;
1721
1722 return VK_FALSE;
1723 }
1724
1725 static vector<char> readFile(const string& filename) {
1726 ifstream file(filename, ios::ate | ios::binary);
1727
1728 if (!file.is_open()) {
1729 throw runtime_error("failed to open file!");
1730 }
1731
1732 size_t fileSize = (size_t) file.tellg();
1733 vector<char> buffer(fileSize);
1734
1735 file.seekg(0);
1736 file.read(buffer.data(), fileSize);
1737
1738 file.close();
1739
1740 return buffer;
1741 }
1742};
1743
1744int main(int argc, char* argv[]) {
1745
1746#ifdef NDEBUG
1747 cout << "DEBUGGING IS OFF" << endl;
1748#else
1749 cout << "DEBUGGING IS ON" << endl;
1750#endif
1751
1752 cout << "Starting Vulkan game..." << endl;
1753
1754 VulkanGame game;
1755
1756 try {
1757 game.run();
1758 } catch (const exception& e) {
1759 cerr << e.what() << endl;
1760 return EXIT_FAILURE;
1761 }
1762
1763 cout << "Finished running the game" << endl;
1764
1765 return EXIT_SUCCESS;
1766}
Note: See TracBrowser for help on using the repository browser.