source: opengl-game/vulkan-ref.cpp@ 34bdf3a

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

In vulkangame, create Vulkan synchronization objects for rendering

  • Property mode set to 100644
File size: 84.3 KB
RevLine 
[f00ee54]1#define STB_IMAGE_IMPLEMENTATION
2#include "stb_image.h" // TODO: Probably switch to SDL_image
[826df16]3
4//#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
[03f4c64]5
6#define GLM_FORCE_RADIANS
[fba08f2]7#define GLM_FORCE_DEPTH_ZERO_TO_ONE
[f00ee54]8
[80edd70]9#include <glm/glm.hpp>
[de32fda]10#include <glm/gtc/matrix_transform.hpp>
[03f4c64]11
12#include <iostream>
[80edd70]13#include <fstream>
14#include <algorithm>
15#include <vector>
16#include <array>
[0e6ecf3]17#include <set>
[80edd70]18#include <optional>
[de32fda]19#include <chrono>
[03f4c64]20
[7fc5e27]21#include "consts.hpp"
[203ab1b]22#include "utils.hpp"
[03f4c64]23
[f00ee54]24#include "game-gui-sdl.hpp"
25
26using namespace std;
27using namespace glm;
[e1a7f5a]28
[34bdf3a]29/*** START OF REFACTORED CODE ***/
[826df16]30const int SCREEN_WIDTH = 800;
31const int SCREEN_HEIGHT = 600;
32
[47bff4c]33const int MAX_FRAMES_IN_FLIGHT = 2;
34
[826df16]35#ifdef NDEBUG
36 const bool enableValidationLayers = false;
37#else
38 const bool enableValidationLayers = true;
39#endif
40
[bfd620e]41const vector<const char*> validationLayers = {
42 "VK_LAYER_KHRONOS_validation"
43};
44
45const vector<const char*> deviceExtensions = {
46 VK_KHR_SWAPCHAIN_EXTENSION_NAME
47};
48
[909b51a]49struct QueueFamilyIndices {
50 optional<uint32_t> graphicsFamily;
[b3671b5]51 optional<uint32_t> presentFamily;
[909b51a]52
53 bool isComplete() {
[b3671b5]54 return graphicsFamily.has_value() && presentFamily.has_value();
[909b51a]55 }
56};
57
[bfd620e]58struct SwapChainSupportDetails {
59 VkSurfaceCapabilitiesKHR capabilities;
60 vector<VkSurfaceFormatKHR> formats;
61 vector<VkPresentModeKHR> presentModes;
62};
63
[80edd70]64struct Vertex {
[adcd252]65 glm::vec3 pos;
[80edd70]66 glm::vec3 color;
[fba08f2]67 glm::vec2 texCoord;
[f00ee54]68};
[80edd70]69
[f00ee54]70struct OverlayVertex {
71 glm::vec3 pos;
72 glm::vec2 texCoord;
[80edd70]73};
[5b02676]74/*** END OF REFACTORED CODE ***/
[80edd70]75
[de32fda]76struct UniformBufferObject {
[f00ee54]77 alignas(16) mat4 model;
78 alignas(16) mat4 view;
79 alignas(16) mat4 proj;
[de32fda]80};
81
[b794178]82/*** START OF REFACTORED CODE ***/
[721e8be]83struct DescriptorInfo {
84 VkDescriptorType type;
85 VkShaderStageFlags stageFlags;
86
87 vector<VkDescriptorBufferInfo>* bufferDataList;
88 VkDescriptorImageInfo* imageData;
89};
[b794178]90/*** END OF REFACTORED CODE ***/
[721e8be]91
[b8b32bd]92struct GraphicsPipelineInfo {
[b794178]93/*** START OF REFACTORED CODE ***/
[d22ae72]94 VkPipelineLayout pipelineLayout;
[b8b32bd]95 VkPipeline pipeline;
96
[f00ee54]97 VkVertexInputBindingDescription bindingDescription;
98 vector<VkVertexInputAttributeDescription> attributeDescriptions;
99
[721e8be]100 vector<DescriptorInfo> descriptorInfoList;
101
[d22ae72]102 VkDescriptorPool descriptorPool;
103 VkDescriptorSetLayout descriptorSetLayout;
104 vector<VkDescriptorSet> descriptorSets;
[b794178]105/*** END OF REFACTORED CODE ***/
[d22ae72]106
[f00ee54]107 size_t numVertices; // Currently unused
[7563b8a]108 size_t vertexCapacity;
[c8b0357]109 VkBuffer vertexBuffer;
110 VkDeviceMemory vertexBufferMemory;
111
[f00ee54]112 size_t numIndices;
[7563b8a]113 size_t indexCapacity;
[c8b0357]114 VkBuffer indexBuffer;
115 VkDeviceMemory indexBufferMemory;
[80edd70]116};
117
[cabdd5c]118/*** START OF REFACTORED CODE ***/
[b6127d2]119VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
120 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
121 const VkAllocationCallbacks* pAllocator,
122 VkDebugUtilsMessengerEXT* pDebugMessenger) {
[621664a]123 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
[b6127d2]124
125 if (func != nullptr) {
126 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
127 } else {
128 return VK_ERROR_EXTENSION_NOT_PRESENT;
129 }
130}
131
[80de39d]132void DestroyDebugUtilsMessengerEXT(VkInstance instance,
133 VkDebugUtilsMessengerEXT debugMessenger,
134 const VkAllocationCallbacks* pAllocator) {
[621664a]135 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
[80de39d]136
137 if (func != nullptr) {
138 func(instance, debugMessenger, pAllocator);
139 }
140}
141
[826df16]142class VulkanGame {
143 public:
144 void run() {
145 if (initWindow() == RTWO_ERROR) {
146 return;
147 }
148 initVulkan();
149 mainLoop();
150 cleanup();
151 }
[621664a]152
[826df16]153 private:
[98f3232]154 GameGui* gui = new GameGui_SDL();
[cabdd5c]155
[c8c6da8]156 SDL_version sdlVersion;
[80de39d]157 SDL_Window* window = nullptr;
[5f3dba8]158 SDL_Renderer* gRenderer = nullptr;
159 SDL_Texture* uiOverlay = nullptr;
[b794178]160/*** END OF REFACTORED CODE ***/
[5f3dba8]161
162 TTF_Font* gFont = nullptr;
163 SDL_Texture* uiText = nullptr;
164 SDL_Texture* uiImage = nullptr;
165
[cabdd5c]166/*** START OF REFACTORED CODE ***/
[826df16]167 VkInstance instance;
[b6127d2]168 VkDebugUtilsMessengerEXT debugMessenger;
[b3671b5]169 VkSurfaceKHR surface;
[5f3dba8]170
[909b51a]171 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
172 VkDevice device;
[b3671b5]173
[909b51a]174 VkQueue graphicsQueue;
[b3671b5]175 VkQueue presentQueue;
[826df16]176
[bfd620e]177 VkSwapchainKHR swapChain;
178 vector<VkImage> swapChainImages;
179 VkFormat swapChainImageFormat;
[771b33a]180 VkExtent2D swapChainExtent;
[bfd620e]181 vector<VkImageView> swapChainImageViews;
[621664a]182 vector<VkFramebuffer> swapChainFramebuffers;
183
[be34c9a]184 VkRenderPass renderPass;
[1187ef5]185
[f5d5686]186 VkCommandPool commandPool;
[1187ef5]187 vector<VkCommandBuffer> commandBuffers;
188
189 // The images and the sampler are used to store data for specific attributes. I probably
190 // want to keep them separate from the GraphicsPipelineInfo objects and start passing
191 // references to them once I start defining uniform and varying attributes in GraphicsPipelineInfo objects
[f5d5686]192
[adcd252]193 VkImage depthImage;
194 VkDeviceMemory depthImageMemory;
195 VkImageView depthImageView;
196
[f5d5686]197 VkImage textureImage;
198 VkDeviceMemory textureImageMemory;
[fba08f2]199 VkImageView textureImageView;
[69dccfe]200
[e1a7f5a]201 VkImage sdlOverlayImage;
202 VkDeviceMemory sdlOverlayImageMemory;
203 VkImageView sdlOverlayImageView;
204
[fba08f2]205 VkSampler textureSampler;
[f5d5686]206
[1187ef5]207 // These are currently to store the MVP matrix
208 // I should figure out if it makes sense to use them for other uniforms in the future
[b794178]209 // If not, I should rename them to better indicate their purpose.
[1187ef5]210 // I should also decide if I can use these for all shaders, or if I need a separapte set of buffers for each one
[de32fda]211 vector<VkBuffer> uniformBuffers;
212 vector<VkDeviceMemory> uniformBuffersMemory;
213
[721e8be]214 VkDescriptorImageInfo sceneImageInfo;
215 VkDescriptorImageInfo overlayImageInfo;
216
217 vector<VkDescriptorBufferInfo> uniformBufferInfoList;
218
[1187ef5]219 GraphicsPipelineInfo scenePipeline;
220 GraphicsPipelineInfo overlayPipeline;
[47bff4c]221
222 vector<VkSemaphore> imageAvailableSemaphores;
223 vector<VkSemaphore> renderFinishedSemaphores;
224 vector<VkFence> inFlightFences;
[34bdf3a]225/*** END OF REFACTORED CODE ***/
[47bff4c]226
227 size_t currentFrame = 0;
[ebeb3aa]228
[7563b8a]229 size_t numPlanes = 0; // temp
230
[0e09340]231/*** START OF REFACTORED CODE ***/
[75108ef]232 bool framebufferResized = false;
233
[826df16]234 bool initWindow() {
[7fc5e27]235 if (gui->init() == RTWO_ERROR) {
[826df16]236 cout << "UI library could not be initialized!" << endl;
[5f3dba8]237 cout << SDL_GetError() << endl;
[826df16]238 return RTWO_ERROR;
[5f3dba8]239 }
240 cout << "GUI init succeeded" << endl;
[826df16]241
[91c89f7]242 window = (SDL_Window*) gui->createWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT, true);
[5f3dba8]243 if (window == nullptr) {
244 cout << "Window could not be created!" << endl;
245 return RTWO_ERROR;
246 }
247
248 gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
249 if (gRenderer == nullptr) {
250 cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
251 return RTWO_ERROR;
252 }
253
[c8c6da8]254 SDL_VERSION(&sdlVersion);
255
256 // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
257 // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
258 // I tried building sdl 2.0.10 (and sdl_image and sdl_ttf) from source on Mac, but had some issues, so this is easier
259 // until the homebrew recipe is updated
260 if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
261 uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
262 } else {
263 uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
264 }
265
[5f3dba8]266 if (uiOverlay == nullptr) {
267 cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
268 }
269 if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
270 cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
271 }
[b794178]272/*** END OF REFACTORED CODE ***/
[5f3dba8]273
274 gFont = TTF_OpenFont("fonts/lazy.ttf", 28);
275 if (gFont == nullptr) {
276 cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
277 return RTWO_ERROR;
278 }
279
280 SDL_Color textColor = { 0, 0, 0 };
281
282 SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great sucess!", textColor);
283 if (textSurface == nullptr) {
284 cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
285 return RTWO_ERROR;
286 }
287
288 uiText = SDL_CreateTextureFromSurface(gRenderer, textSurface);
289 if (uiText == nullptr) {
290 cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
291 SDL_FreeSurface(textSurface);
292 return RTWO_ERROR;
293 }
294
295 SDL_FreeSurface(textSurface);
296
297 // TODO: Load a PNG instead
298 SDL_Surface* uiImageSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
299 if (uiImageSurface == nullptr) {
300 cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
301 return RTWO_ERROR;
[826df16]302 }
[5f3dba8]303
304 uiImage = SDL_CreateTextureFromSurface(gRenderer, uiImageSurface);
305 if (uiImage == nullptr) {
306 cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
307 SDL_FreeSurface(uiImageSurface);
308 return RTWO_ERROR;
309 }
310
311 SDL_FreeSurface(uiImageSurface);
312
313 return RTWO_SUCCESS;
[826df16]314 }
315
[cabdd5c]316/*** START OF REFACTORED CODE ***/
[826df16]317 void initVulkan() {
318 createInstance();
[7dcd925]319 setupDebugMessenger();
[b3671b5]320 createSurface();
[909b51a]321 pickPhysicalDevice();
322 createLogicalDevice();
[bfd620e]323 createSwapChain();
324 createImageViews();
[be34c9a]325 createRenderPass();
[d22ae72]326
[47bff4c]327 createCommandPool();
[1187ef5]328
[69dccfe]329 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
[e1a7f5a]330 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView);
[fba08f2]331 createTextureSampler();
[c8b0357]332
[721e8be]333 sceneImageInfo = {};
334 sceneImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
335 sceneImageInfo.imageView = textureImageView;
336 sceneImageInfo.sampler = textureSampler;
337
338 overlayImageInfo = {};
339 overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
340 overlayImageInfo.imageView = sdlOverlayImageView;
341 overlayImageInfo.sampler = textureSampler;
[b794178]342/*** END OF REFACTORED CODE ***/
[721e8be]343
[a0da009]344 // SHADER-SPECIFIC STUFF STARTS HERE
345
[f00ee54]346 vector<Vertex> sceneVertices = {
[c8b0357]347 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
348 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
349 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
350 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
351
352 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
353 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
354 {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
355 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
[f00ee54]356 };
357 vector<uint16_t> sceneIndices = {
[e5d4aca]358 0, 1, 2, 2, 3, 0,
359 4, 5, 6, 6, 7, 4
[f00ee54]360 };
361
362 initGraphicsPipelineInfo(scenePipeline,
363 sceneVertices.data(), sizeof(Vertex), sceneVertices.size(),
364 sceneIndices.data(), sizeof(uint16_t), sceneIndices.size());
365
[5b02676]366/*** START OF REFACTORED CODE ***/
[f00ee54]367 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
368 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
369 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
370
[721e8be]371 addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList, nullptr);
372 addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &sceneImageInfo);
373
374 createDescriptorSetLayout(scenePipeline);
[b794178]375/*** END OF REFACTORED CODE ***/
[c8b0357]376
[7563b8a]377 numPlanes = 2;
[f00ee54]378
379 vector<OverlayVertex> overlayVertices = {
380 {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
381 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
382 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
383 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}
384 };
385 vector<uint16_t> overlayIndices = {
386 0, 1, 2, 2, 3, 0
387 };
388
389 initGraphicsPipelineInfo(overlayPipeline,
390 overlayVertices.data(), sizeof(OverlayVertex), overlayVertices.size(),
391 overlayIndices.data(), sizeof(uint16_t), overlayIndices.size());
392
[5b02676]393/*** START OF REFACTORED CODE ***/
[f00ee54]394 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
395 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
396
[721e8be]397 addDescriptorInfo(overlayPipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &overlayImageInfo);
398
399 createDescriptorSetLayout(overlayPipeline);
[c8b0357]400
[e5d4aca]401 createBufferResources();
[d22ae72]402
[47bff4c]403 createSyncObjects();
[826df16]404 }
405
406 void createInstance() {
[b6127d2]407 if (enableValidationLayers && !checkValidationLayerSupport()) {
408 throw runtime_error("validation layers requested, but not available!");
409 }
410
[826df16]411 VkApplicationInfo appInfo = {};
412 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
413 appInfo.pApplicationName = "Vulkan Game";
414 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
415 appInfo.pEngineName = "No Engine";
416 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
417 appInfo.apiVersion = VK_API_VERSION_1_0;
418
419 VkInstanceCreateInfo createInfo = {};
420 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
421 createInfo.pApplicationInfo = &appInfo;
422
[a8f0577]423 vector<const char*> extensions = getRequiredExtensions();
[b6127d2]424 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
425 createInfo.ppEnabledExtensionNames = extensions.data();
[826df16]426
[8667f76]427 cout << endl << "Extensions:" << endl;
[b3671b5]428 for (const char* extensionName : extensions) {
429 cout << extensionName << endl;
430 }
431 cout << endl;
432
[80de39d]433 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
[b6127d2]434 if (enableValidationLayers) {
435 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
436 createInfo.ppEnabledLayerNames = validationLayers.data();
[80de39d]437
438 populateDebugMessengerCreateInfo(debugCreateInfo);
439 createInfo.pNext = &debugCreateInfo;
[b6127d2]440 } else {
441 createInfo.enabledLayerCount = 0;
[80de39d]442
443 createInfo.pNext = nullptr;
[b6127d2]444 }
[826df16]445
446 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
447 throw runtime_error("failed to create instance!");
448 }
449 }
450
[621664a]451 bool checkValidationLayerSupport() {
452 uint32_t layerCount;
453 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
454
455 vector<VkLayerProperties> availableLayers(layerCount);
456 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
457
458 for (const char* layerName : validationLayers) {
459 bool layerFound = false;
460
461 for (const auto& layerProperties : availableLayers) {
462 if (strcmp(layerName, layerProperties.layerName) == 0) {
463 layerFound = true;
464 break;
465 }
466 }
467
468 if (!layerFound) {
469 return false;
470 }
471 }
472
473 return true;
474 }
475
476 vector<const char*> getRequiredExtensions() {
[7fc5e27]477 vector<const char*> extensions = gui->getRequiredExtensions();
[621664a]478
479 if (enableValidationLayers) {
480 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
481 }
482
483 return extensions;
484 }
485
[80de39d]486 void setupDebugMessenger() {
487 if (!enableValidationLayers) return;
488
489 VkDebugUtilsMessengerCreateInfoEXT createInfo;
490 populateDebugMessengerCreateInfo(createInfo);
[b6127d2]491
492 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
[621664a]493 throw runtime_error("failed to set up debug messenger!");
[b6127d2]494 }
495 }
496
[621664a]497 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
498 createInfo = {};
499 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
500 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;
501 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;
502 createInfo.pfnUserCallback = debugCallback;
503 }
504
[b3671b5]505 void createSurface() {
[7fc5e27]506 if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
[b3671b5]507 throw runtime_error("failed to create window surface!");
508 }
509 }
510
[909b51a]511 void pickPhysicalDevice() {
512 uint32_t deviceCount = 0;
513 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
514
515 if (deviceCount == 0) {
516 throw runtime_error("failed to find GPUs with Vulkan support!");
517 }
518
519 vector<VkPhysicalDevice> devices(deviceCount);
520 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
521
522 cout << endl << "Graphics cards:" << endl;
523 for (const VkPhysicalDevice& device : devices) {
524 if (isDeviceSuitable(device)) {
525 physicalDevice = device;
526 break;
527 }
528 }
529 cout << endl;
530
531 if (physicalDevice == VK_NULL_HANDLE) {
532 throw runtime_error("failed to find a suitable GPU!");
533 }
534 }
535
536 bool isDeviceSuitable(VkPhysicalDevice device) {
537 VkPhysicalDeviceProperties deviceProperties;
538 vkGetPhysicalDeviceProperties(device, &deviceProperties);
539
540 cout << "Device: " << deviceProperties.deviceName << endl;
541
542 QueueFamilyIndices indices = findQueueFamilies(device);
[bfd620e]543 bool extensionsSupported = checkDeviceExtensionSupport(device);
544 bool swapChainAdequate = false;
545
546 if (extensionsSupported) {
547 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
548 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
549 }
550
[fba08f2]551 VkPhysicalDeviceFeatures supportedFeatures;
552 vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
553
554 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
[bfd620e]555 }
556
557 bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
558 uint32_t extensionCount;
559 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
560
561 vector<VkExtensionProperties> availableExtensions(extensionCount);
562 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
563
564 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
565
566 for (const auto& extension : availableExtensions) {
567 requiredExtensions.erase(extension.extensionName);
568 }
569
570 return requiredExtensions.empty();
[909b51a]571 }
572
573 void createLogicalDevice() {
574 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
575
[b3671b5]576 vector<VkDeviceQueueCreateInfo> queueCreateInfos;
577 set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
[909b51a]578
579 float queuePriority = 1.0f;
[b3671b5]580 for (uint32_t queueFamily : uniqueQueueFamilies) {
581 VkDeviceQueueCreateInfo queueCreateInfo = {};
582 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
583 queueCreateInfo.queueFamilyIndex = queueFamily;
584 queueCreateInfo.queueCount = 1;
585 queueCreateInfo.pQueuePriorities = &queuePriority;
586
587 queueCreateInfos.push_back(queueCreateInfo);
588 }
[909b51a]589
590 VkPhysicalDeviceFeatures deviceFeatures = {};
[fba08f2]591 deviceFeatures.samplerAnisotropy = VK_TRUE;
[909b51a]592
593 VkDeviceCreateInfo createInfo = {};
594 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
[621664a]595 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
[b3671b5]596 createInfo.pQueueCreateInfos = queueCreateInfos.data();
[909b51a]597
598 createInfo.pEnabledFeatures = &deviceFeatures;
599
[bfd620e]600 createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
601 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
[909b51a]602
603 // These fields are ignored by up-to-date Vulkan implementations,
604 // but it's a good idea to set them for backwards compatibility
605 if (enableValidationLayers) {
606 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
607 createInfo.ppEnabledLayerNames = validationLayers.data();
608 } else {
609 createInfo.enabledLayerCount = 0;
610 }
611
612 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
613 throw runtime_error("failed to create logical device!");
614 }
615
616 vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
[b3671b5]617 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
[909b51a]618 }
619
[621664a]620 void createSwapChain() {
621 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
[a8f0577]622
[621664a]623 VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
624 VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
625 VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
[a8f0577]626
[621664a]627 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
628 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
629 imageCount = swapChainSupport.capabilities.maxImageCount;
[a8f0577]630 }
631
[621664a]632 VkSwapchainCreateInfoKHR createInfo = {};
633 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
634 createInfo.surface = surface;
635 createInfo.minImageCount = imageCount;
636 createInfo.imageFormat = surfaceFormat.format;
637 createInfo.imageColorSpace = surfaceFormat.colorSpace;
638 createInfo.imageExtent = extent;
639 createInfo.imageArrayLayers = 1;
640 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
[909b51a]641
[621664a]642 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
643 uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
[b3671b5]644
[621664a]645 if (indices.graphicsFamily != indices.presentFamily) {
646 createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
647 createInfo.queueFamilyIndexCount = 2;
648 createInfo.pQueueFamilyIndices = queueFamilyIndices;
649 } else {
650 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
651 createInfo.queueFamilyIndexCount = 0;
652 createInfo.pQueueFamilyIndices = nullptr;
653 }
[b3671b5]654
[621664a]655 createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
656 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
657 createInfo.presentMode = presentMode;
658 createInfo.clipped = VK_TRUE;
659 createInfo.oldSwapchain = VK_NULL_HANDLE;
[909b51a]660
[621664a]661 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
662 throw runtime_error("failed to create swap chain!");
[909b51a]663 }
664
[621664a]665 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
666 swapChainImages.resize(imageCount);
667 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
668
669 swapChainImageFormat = surfaceFormat.format;
670 swapChainExtent = extent;
[909b51a]671 }
672
[bfd620e]673 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
674 SwapChainSupportDetails details;
675
676 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
677
678 uint32_t formatCount;
679 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
680
681 if (formatCount != 0) {
682 details.formats.resize(formatCount);
683 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
684 }
685
686 uint32_t presentModeCount;
687 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
688
689 if (presentModeCount != 0) {
690 details.presentModes.resize(presentModeCount);
691 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
692 }
693
694 return details;
695 }
696
697 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
698 for (const auto& availableFormat : availableFormats) {
699 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
700 return availableFormat;
701 }
702 }
703
704 return availableFormats[0];
705 }
706
707 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
708 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
709
710 for (const auto& availablePresentMode : availablePresentModes) {
711 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
712 return availablePresentMode;
[621664a]713 }
714 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
[bfd620e]715 bestMode = availablePresentMode;
716 }
717 }
718
719 return bestMode;
720 }
721
722 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
723 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
724 return capabilities.currentExtent;
[cabdd5c]725 } else {
[75108ef]726 VkExtent2D actualExtent = {
[cabdd5c]727 static_cast<uint32_t>(gui->getWindowWidth()),
728 static_cast<uint32_t>(gui->getWindowHeight())
[75108ef]729 };
[bfd620e]730
[f00ee54]731 actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
732 actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
[bfd620e]733
734 return actualExtent;
735 }
736 }
737
738 void createImageViews() {
739 swapChainImageViews.resize(swapChainImages.size());
740
[621664a]741 for (size_t i = 0; i < swapChainImages.size(); i++) {
[adcd252]742 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
[bfd620e]743 }
744 }
745
[be34c9a]746 void createRenderPass() {
747 VkAttachmentDescription colorAttachment = {};
748 colorAttachment.format = swapChainImageFormat;
749 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
750 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
751 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
752 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
753 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
754 colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
755 colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
756
757 VkAttachmentReference colorAttachmentRef = {};
758 colorAttachmentRef.attachment = 0;
759 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
760
[adcd252]761 VkAttachmentDescription depthAttachment = {};
762 depthAttachment.format = findDepthFormat();
763 depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
764 depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
765 depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
766 depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
767 depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
768 depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
769 depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
770
771 VkAttachmentReference depthAttachmentRef = {};
772 depthAttachmentRef.attachment = 1;
773 depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
774
[be34c9a]775 VkSubpassDescription subpass = {};
776 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
777 subpass.colorAttachmentCount = 1;
778 subpass.pColorAttachments = &colorAttachmentRef;
[adcd252]779 subpass.pDepthStencilAttachment = &depthAttachmentRef;
[be34c9a]780
[621664a]781 VkSubpassDependency dependency = {};
[47bff4c]782 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
783 dependency.dstSubpass = 0;
784 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
785 dependency.srcAccessMask = 0;
786 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
787 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
788
[adcd252]789 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
[be34c9a]790 VkRenderPassCreateInfo renderPassInfo = {};
791 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
[adcd252]792 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
793 renderPassInfo.pAttachments = attachments.data();
[be34c9a]794 renderPassInfo.subpassCount = 1;
795 renderPassInfo.pSubpasses = &subpass;
[47bff4c]796 renderPassInfo.dependencyCount = 1;
797 renderPassInfo.pDependencies = &dependency;
[be34c9a]798
799 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
800 throw runtime_error("failed to create render pass!");
801 }
802 }
803
[f00ee54]804 void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
805 const void* vertexData, int vertexSize, size_t numVertices,
806 const void* indexData, int indexSize, size_t numIndices) {
807 // Since there is only one array of vertex data, we use binding = 0
808 // I'll probably do that for the foreseeable future
809 // I can calculate the stride myself given info about all the varying attributes
810
811 info.bindingDescription.binding = 0;
812 info.bindingDescription.stride = vertexSize;
813 info.bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
[5b02676]814/*** END OF REFACTORED CODE ***/
[f00ee54]815
816 info.numVertices = numVertices;
[7563b8a]817 info.vertexCapacity = numVertices * 2;
818 createVertexBuffer(info, vertexData, vertexSize);
[f00ee54]819
820 info.numIndices = numIndices;
[7563b8a]821 info.indexCapacity = numIndices * 2;
822 createIndexBuffer(info, indexData, indexSize);
[f00ee54]823 }
824
825 void addAttributeDescription(GraphicsPipelineInfo& info, VkFormat format, size_t offset) {
826 VkVertexInputAttributeDescription attributeDesc = {};
827
828 attributeDesc.binding = 0;
829 attributeDesc.location = info.attributeDescriptions.size();
830 attributeDesc.format = format;
831 attributeDesc.offset = offset;
832
833 info.attributeDescriptions.push_back(attributeDesc);
834 }
835
[b794178]836/*** START OF REFACTORED CODE ***/
[721e8be]837 void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) {
838 info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData });
839 }
840
841 void createDescriptorSetLayout(GraphicsPipelineInfo& info) {
842 vector<VkDescriptorSetLayoutBinding> bindings(info.descriptorInfoList.size());
843
844 for (size_t i = 0; i < bindings.size(); i++) {
845 bindings[i].binding = i;
846 bindings[i].descriptorCount = 1;
847 bindings[i].descriptorType = info.descriptorInfoList[i].type;
848 bindings[i].stageFlags = info.descriptorInfoList[i].stageFlags;
849 bindings[i].pImmutableSamplers = nullptr;
850 }
851
852 VkDescriptorSetLayoutCreateInfo layoutInfo = {};
853 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
854 layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
855 layoutInfo.pBindings = bindings.data();
856
857 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
858 throw runtime_error("failed to create descriptor set layout!");
859 }
860 }
861
[d22ae72]862 void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) {
[7d2b0b9]863 vector<char> vertShaderCode = readFile(vertShaderFile);
864 vector<char> fragShaderCode = readFile(fragShaderFile);
[e09ad38]865
866 VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
867 VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
868
869 VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
870 vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
871 vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
872 vertShaderStageInfo.module = vertShaderModule;
873 vertShaderStageInfo.pName = "main";
874
875 VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
876 fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
877 fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
878 fragShaderStageInfo.module = fragShaderModule;
879 fragShaderStageInfo.pName = "main";
880
881 VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
882
[84216c7]883 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
884 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
[80edd70]885
886 vertexInputInfo.vertexBindingDescriptionCount = 1;
[f00ee54]887 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(info.attributeDescriptions.size());
888 vertexInputInfo.pVertexBindingDescriptions = &info.bindingDescription;
889 vertexInputInfo.pVertexAttributeDescriptions = info.attributeDescriptions.data();
[84216c7]890
891 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
892 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
893 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
894 inputAssembly.primitiveRestartEnable = VK_FALSE;
895
896 VkViewport viewport = {};
897 viewport.x = 0.0f;
898 viewport.y = 0.0f;
899 viewport.width = (float) swapChainExtent.width;
900 viewport.height = (float) swapChainExtent.height;
901 viewport.minDepth = 0.0f;
902 viewport.maxDepth = 1.0f;
903
904 VkRect2D scissor = {};
905 scissor.offset = { 0, 0 };
906 scissor.extent = swapChainExtent;
907
908 VkPipelineViewportStateCreateInfo viewportState = {};
909 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
910 viewportState.viewportCount = 1;
911 viewportState.pViewports = &viewport;
912 viewportState.scissorCount = 1;
913 viewportState.pScissors = &scissor;
914
915 VkPipelineRasterizationStateCreateInfo rasterizer = {};
916 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
917 rasterizer.depthClampEnable = VK_FALSE;
918 rasterizer.rasterizerDiscardEnable = VK_FALSE;
919 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
920 rasterizer.lineWidth = 1.0f;
921 rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
[c7fb883]922 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
[621664a]923 rasterizer.depthBiasEnable = VK_FALSE;
[84216c7]924
925 VkPipelineMultisampleStateCreateInfo multisampling = {};
926 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
927 multisampling.sampleShadingEnable = VK_FALSE;
928 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
929
930 VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
931 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
[69dccfe]932 colorBlendAttachment.blendEnable = VK_TRUE;
933 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
934 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
935 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
936 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
937 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
938 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
[84216c7]939
940 VkPipelineColorBlendStateCreateInfo colorBlending = {};
941 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
942 colorBlending.logicOpEnable = VK_FALSE;
943 colorBlending.logicOp = VK_LOGIC_OP_COPY;
944 colorBlending.attachmentCount = 1;
945 colorBlending.pAttachments = &colorBlendAttachment;
946 colorBlending.blendConstants[0] = 0.0f;
947 colorBlending.blendConstants[1] = 0.0f;
948 colorBlending.blendConstants[2] = 0.0f;
949 colorBlending.blendConstants[3] = 0.0f;
950
[adcd252]951 VkPipelineDepthStencilStateCreateInfo depthStencil = {};
952 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
953 depthStencil.depthTestEnable = VK_TRUE;
954 depthStencil.depthWriteEnable = VK_TRUE;
955 depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
956 depthStencil.depthBoundsTestEnable = VK_FALSE;
957 depthStencil.minDepthBounds = 0.0f;
958 depthStencil.maxDepthBounds = 1.0f;
959 depthStencil.stencilTestEnable = VK_FALSE;
960 depthStencil.front = {};
961 depthStencil.back = {};
962
[84216c7]963 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
964 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
[de32fda]965 pipelineLayoutInfo.setLayoutCount = 1;
[d22ae72]966 pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout;
[84216c7]967 pipelineLayoutInfo.pushConstantRangeCount = 0;
968
[d22ae72]969 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) {
[84216c7]970 throw runtime_error("failed to create pipeline layout!");
971 }
972
[fd70015]973 VkGraphicsPipelineCreateInfo pipelineInfo = {};
974 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
975 pipelineInfo.stageCount = 2;
976 pipelineInfo.pStages = shaderStages;
977 pipelineInfo.pVertexInputState = &vertexInputInfo;
978 pipelineInfo.pInputAssemblyState = &inputAssembly;
979 pipelineInfo.pViewportState = &viewportState;
980 pipelineInfo.pRasterizationState = &rasterizer;
981 pipelineInfo.pMultisampleState = &multisampling;
[adcd252]982 pipelineInfo.pDepthStencilState = &depthStencil;
[fd70015]983 pipelineInfo.pColorBlendState = &colorBlending;
984 pipelineInfo.pDynamicState = nullptr;
[d22ae72]985 pipelineInfo.layout = info.pipelineLayout;
[fd70015]986 pipelineInfo.renderPass = renderPass;
987 pipelineInfo.subpass = 0;
988 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
989 pipelineInfo.basePipelineIndex = -1;
990
[d22ae72]991 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) {
[fd70015]992 throw runtime_error("failed to create graphics pipeline!");
993 }
994
[e09ad38]995 vkDestroyShaderModule(device, vertShaderModule, nullptr);
996 vkDestroyShaderModule(device, fragShaderModule, nullptr);
997 }
998
999 VkShaderModule createShaderModule(const vector<char>& code) {
1000 VkShaderModuleCreateInfo createInfo = {};
1001 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1002 createInfo.codeSize = code.size();
1003 createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
1004
1005 VkShaderModule shaderModule;
1006 if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
1007 throw runtime_error("failed to create shader module!");
1008 }
1009
1010 return shaderModule;
[4befb76]1011 }
1012
[ebeb3aa]1013 void createFramebuffers() {
1014 swapChainFramebuffers.resize(swapChainImageViews.size());
1015
1016 for (size_t i = 0; i < swapChainImageViews.size(); i++) {
[603b5bc]1017 array<VkImageView, 2> attachments = {
[adcd252]1018 swapChainImageViews[i],
1019 depthImageView
[ebeb3aa]1020 };
1021
1022 VkFramebufferCreateInfo framebufferInfo = {};
1023 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1024 framebufferInfo.renderPass = renderPass;
[adcd252]1025 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1026 framebufferInfo.pAttachments = attachments.data();
[ebeb3aa]1027 framebufferInfo.width = swapChainExtent.width;
1028 framebufferInfo.height = swapChainExtent.height;
1029 framebufferInfo.layers = 1;
1030
1031 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
1032 throw runtime_error("failed to create framebuffer!");
1033 }
1034 }
1035 }
1036
[47bff4c]1037 void createCommandPool() {
1038 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
1039
1040 VkCommandPoolCreateInfo poolInfo = {};
1041 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1042 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
1043 poolInfo.flags = 0;
1044
1045 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
[621664a]1046 throw runtime_error("failed to create graphics command pool!");
[47bff4c]1047 }
1048 }
1049
[621664a]1050 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
1051 QueueFamilyIndices indices;
1052
1053 uint32_t queueFamilyCount = 0;
1054 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
1055
1056 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
1057 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
1058
1059 int i = 0;
1060 for (const auto& queueFamily : queueFamilies) {
1061 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
1062 indices.graphicsFamily = i;
1063 }
1064
1065 VkBool32 presentSupport = false;
1066 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
1067
1068 if (queueFamily.queueCount > 0 && presentSupport) {
1069 indices.presentFamily = i;
1070 }
1071
1072 if (indices.isComplete()) {
1073 break;
1074 }
1075
1076 i++;
1077 }
1078
1079 return indices;
1080 }
1081
[adcd252]1082 void createDepthResources() {
1083 VkFormat depthFormat = findDepthFormat();
1084
1085 createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
1086 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
1087 depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
1088
1089 transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1090 }
1091
1092 VkFormat findDepthFormat() {
1093 return findSupportedFormat(
1094 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
1095 VK_IMAGE_TILING_OPTIMAL,
1096 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
1097 );
1098 }
1099
1100 VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling,
1101 VkFormatFeatureFlags features) {
1102 for (VkFormat format : candidates) {
1103 VkFormatProperties props;
1104 vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
1105
1106 if (tiling == VK_IMAGE_TILING_LINEAR &&
1107 (props.linearTilingFeatures & features) == features) {
1108 return format;
1109 } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
1110 (props.optimalTilingFeatures & features) == features) {
1111 return format;
1112 }
1113 }
1114
1115 throw runtime_error("failed to find supported format!");
1116 }
1117
1118 bool hasStencilComponent(VkFormat format) {
1119 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
1120 }
1121
[69dccfe]1122 void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
[eea05dd]1123 int texWidth, texHeight, texChannels;
1124
[69dccfe]1125 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
[eea05dd]1126 VkDeviceSize imageSize = texWidth * texHeight * 4;
1127
1128 if (!pixels) {
1129 throw runtime_error("failed to load texture image!");
1130 }
1131
1132 VkBuffer stagingBuffer;
1133 VkDeviceMemory stagingBufferMemory;
1134
1135 createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1136 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1137 stagingBuffer, stagingBufferMemory);
1138
1139 void* data;
1140
1141 vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
1142 memcpy(data, pixels, static_cast<size_t>(imageSize));
1143 vkUnmapMemory(device, stagingBufferMemory);
1144
1145 stbi_image_free(pixels);
1146
1147 createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1148 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
[69dccfe]1149 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
[eea05dd]1150
[69dccfe]1151 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
1152 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
1153 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
[eea05dd]1154
1155 vkDestroyBuffer(device, stagingBuffer, nullptr);
[f5d5686]1156 vkFreeMemory(device, stagingBufferMemory, nullptr);
[69dccfe]1157
1158 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
[eea05dd]1159 }
1160
[e1a7f5a]1161 void createImageResourcesFromSDLTexture(SDL_Texture* texture, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
1162 int a, w, h;
1163
1164 // I only need this here for the width and height, which are constants, so just use those instead
1165 SDL_QueryTexture(texture, nullptr, &a, &w, &h);
1166
1167 createImage(w, h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1168 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1169 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
1170
1171 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
1172 }
[b794178]1173/*** END OF REFACTORED CODE ***/
[e1a7f5a]1174
1175 void populateImageFromSDLTexture(SDL_Texture* texture, VkImage& image) {
[f00ee54]1176 int a, w, h;
[e1a7f5a]1177
1178 SDL_QueryTexture(texture, nullptr, &a, &w, &h);
1179
1180 VkDeviceSize imageSize = w * h * 4;
1181 unsigned char* pixels = new unsigned char[imageSize];
1182
1183 SDL_RenderReadPixels(gRenderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4);
1184
1185 VkBuffer stagingBuffer;
1186 VkDeviceMemory stagingBufferMemory;
1187
1188 createBuffer(imageSize,
1189 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1190 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1191 stagingBuffer, stagingBufferMemory);
1192
1193 void* data;
1194
1195 vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
1196 memcpy(data, pixels, static_cast<size_t>(imageSize));
1197
1198 VkMappedMemoryRange mappedMemoryRange = {};
1199 mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1200 mappedMemoryRange.memory = stagingBufferMemory;
1201 mappedMemoryRange.offset = 0;
1202 mappedMemoryRange.size = VK_WHOLE_SIZE;
1203
1204 // TODO: Should probably check that the function succeeded
1205 vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange);
1206 vkUnmapMemory(device, stagingBufferMemory);
1207
[8a40f4b]1208 delete[] pixels;
1209
[e1a7f5a]1210 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
1211 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(w), static_cast<uint32_t>(h));
1212 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1213
1214 vkDestroyBuffer(device, stagingBuffer, nullptr);
1215 vkFreeMemory(device, stagingBufferMemory, nullptr);
1216 }
1217
[603b5bc]1218/*** START OF REFACTORED CODE ***/
[621664a]1219 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
1220 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
[eea05dd]1221 VkImageCreateInfo imageInfo = {};
1222 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1223 imageInfo.imageType = VK_IMAGE_TYPE_2D;
1224 imageInfo.extent.width = width;
1225 imageInfo.extent.height = height;
1226 imageInfo.extent.depth = 1;
1227 imageInfo.mipLevels = 1;
1228 imageInfo.arrayLayers = 1;
1229 imageInfo.format = format;
1230 imageInfo.tiling = tiling;
1231 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1232 imageInfo.usage = usage;
1233 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
1234 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1235
1236 if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
1237 throw runtime_error("failed to create image!");
1238 }
1239
1240 VkMemoryRequirements memRequirements;
1241 vkGetImageMemoryRequirements(device, image, &memRequirements);
1242
[621664a]1243 VkMemoryAllocateInfo allocInfo = {};
[eea05dd]1244 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1245 allocInfo.allocationSize = memRequirements.size;
1246 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1247
1248 if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
1249 throw runtime_error("failed to allocate image memory!");
1250 }
1251
1252 vkBindImageMemory(device, image, imageMemory, 0);
1253 }
1254
[621664a]1255 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
[eea05dd]1256 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1257
1258 VkImageMemoryBarrier barrier = {};
1259 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1260 barrier.oldLayout = oldLayout;
1261 barrier.newLayout = newLayout;
1262 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1263 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1264 barrier.image = image;
[adcd252]1265
1266 if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1267 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1268
1269 if (hasStencilComponent(format)) {
1270 barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
1271 }
1272 } else {
1273 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1274 }
1275
[eea05dd]1276 barrier.subresourceRange.baseMipLevel = 0;
1277 barrier.subresourceRange.levelCount = 1;
1278 barrier.subresourceRange.baseArrayLayer = 0;
1279 barrier.subresourceRange.layerCount = 1;
[f5d5686]1280
1281 VkPipelineStageFlags sourceStage;
1282 VkPipelineStageFlags destinationStage;
[eea05dd]1283
1284 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1285 barrier.srcAccessMask = 0;
1286 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1287
1288 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1289 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1290 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1291 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1292 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1293
1294 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1295 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
[adcd252]1296 } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1297 barrier.srcAccessMask = 0;
1298 barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
1299
1300 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1301 destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
[eea05dd]1302 } else {
1303 throw invalid_argument("unsupported layout transition!");
1304 }
1305
1306 vkCmdPipelineBarrier(
1307 commandBuffer,
[f5d5686]1308 sourceStage, destinationStage,
[eea05dd]1309 0,
1310 0, nullptr,
1311 0, nullptr,
1312 1, &barrier
1313 );
1314
1315 endSingleTimeCommands(commandBuffer);
1316 }
1317
1318 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
1319 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1320
1321 VkBufferImageCopy region = {};
1322 region.bufferOffset = 0;
1323 region.bufferRowLength = 0;
1324 region.bufferImageHeight = 0;
1325 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1326 region.imageSubresource.mipLevel = 0;
1327 region.imageSubresource.baseArrayLayer = 0;
1328 region.imageSubresource.layerCount = 1;
1329 region.imageOffset = { 0, 0, 0 };
1330 region.imageExtent = { width, height, 1 };
1331
1332 vkCmdCopyBufferToImage(
1333 commandBuffer,
1334 buffer,
1335 image,
1336 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1337 1,
1338 &region
1339 );
1340
1341 endSingleTimeCommands(commandBuffer);
1342 }
1343
[adcd252]1344 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
[fba08f2]1345 VkImageViewCreateInfo viewInfo = {};
1346 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1347 viewInfo.image = image;
1348 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
1349 viewInfo.format = format;
1350
1351 viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
1352 viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
1353 viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
1354 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
1355
[adcd252]1356 viewInfo.subresourceRange.aspectMask = aspectFlags;
[fba08f2]1357 viewInfo.subresourceRange.baseMipLevel = 0;
1358 viewInfo.subresourceRange.levelCount = 1;
1359 viewInfo.subresourceRange.baseArrayLayer = 0;
1360 viewInfo.subresourceRange.layerCount = 1;
1361
1362 VkImageView imageView;
1363 if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
1364 throw runtime_error("failed to create texture image view!");
1365 }
1366
1367 return imageView;
1368 }
1369
1370 void createTextureSampler() {
1371 VkSamplerCreateInfo samplerInfo = {};
1372 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1373 samplerInfo.magFilter = VK_FILTER_LINEAR;
1374 samplerInfo.minFilter = VK_FILTER_LINEAR;
1375
1376 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1377 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1378 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1379
1380 samplerInfo.anisotropyEnable = VK_TRUE;
1381 samplerInfo.maxAnisotropy = 16;
1382 samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
1383 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1384 samplerInfo.compareEnable = VK_FALSE;
1385 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
1386 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
1387 samplerInfo.mipLodBias = 0.0f;
1388 samplerInfo.minLod = 0.0f;
1389 samplerInfo.maxLod = 0.0f;
1390
1391 if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
1392 throw runtime_error("failed to create texture sampler!");
1393 }
1394 }
[b794178]1395/*** END OF REFACTORED CODE ***/
[fba08f2]1396
[7563b8a]1397 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) {
1398 VkDeviceSize bufferSize = info.numVertices * vertexSize;
1399 VkDeviceSize bufferCapacity = info.vertexCapacity * vertexSize;
1400
[d9ef6ab]1401 VkBuffer stagingBuffer;
1402 VkDeviceMemory stagingBufferMemory;
[621664a]1403 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
[d9ef6ab]1404 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1405 stagingBuffer, stagingBufferMemory);
1406
1407 void* data;
1408 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
[f00ee54]1409 memcpy(data, vertexData, (size_t) bufferSize);
[d9ef6ab]1410 vkUnmapMemory(device, stagingBufferMemory);
1411
[7563b8a]1412 createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
[f00ee54]1413 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory);
[d9ef6ab]1414
[7563b8a]1415 copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0, bufferSize);
[d9ef6ab]1416
1417 vkDestroyBuffer(device, stagingBuffer, nullptr);
1418 vkFreeMemory(device, stagingBufferMemory, nullptr);
1419 }
1420
[7563b8a]1421 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, int indexSize) {
1422 VkDeviceSize bufferSize = info.numIndices * indexSize;
1423 VkDeviceSize bufferCapacity = info.indexCapacity * indexSize;
1424
[cae7a2c]1425 VkBuffer stagingBuffer;
1426 VkDeviceMemory stagingBufferMemory;
1427 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1428 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1429 stagingBuffer, stagingBufferMemory);
1430
1431 void* data;
1432 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
[f00ee54]1433 memcpy(data, indexData, (size_t) bufferSize);
[cae7a2c]1434 vkUnmapMemory(device, stagingBufferMemory);
1435
[7563b8a]1436 createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
[f00ee54]1437 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory);
[cae7a2c]1438
[7563b8a]1439 copyBuffer(stagingBuffer, info.indexBuffer, 0, 0, bufferSize);
[cae7a2c]1440
1441 vkDestroyBuffer(device, stagingBuffer, nullptr);
1442 vkFreeMemory(device, stagingBufferMemory, nullptr);
1443 }
1444
[b794178]1445/*** START OF REFACTORED CODE ***/
[621664a]1446 void createUniformBuffers() {
1447 VkDeviceSize bufferSize = sizeof(UniformBufferObject);
1448
1449 uniformBuffers.resize(swapChainImages.size());
1450 uniformBuffersMemory.resize(swapChainImages.size());
[721e8be]1451 uniformBufferInfoList.resize(swapChainImages.size());
[621664a]1452
1453 for (size_t i = 0; i < swapChainImages.size(); i++) {
1454 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1455 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1456 uniformBuffers[i], uniformBuffersMemory[i]);
[721e8be]1457
1458 uniformBufferInfoList[i].buffer = uniformBuffers[i];
1459 uniformBufferInfoList[i].offset = 0;
1460 uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
[621664a]1461 }
1462 }
1463
1464 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
[80edd70]1465 VkBufferCreateInfo bufferInfo = {};
1466 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
[d9ef6ab]1467 bufferInfo.size = size;
1468 bufferInfo.usage = usage;
[80edd70]1469 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1470
[d9ef6ab]1471 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1472 throw runtime_error("failed to create buffer!");
[80edd70]1473 }
1474
[d9ef6ab]1475 VkMemoryRequirements memRequirements;
1476 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
[80edd70]1477
1478 VkMemoryAllocateInfo allocInfo = {};
1479 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
[d9ef6ab]1480 allocInfo.allocationSize = memRequirements.size;
1481 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
[621664a]1482
[d9ef6ab]1483 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1484 throw runtime_error("failed to allocate buffer memory!");
[80edd70]1485 }
1486
[d9ef6ab]1487 vkBindBufferMemory(device, buffer, bufferMemory, 0);
1488 }
[b794178]1489/*** END OF REFACTORED CODE ***/
[80edd70]1490
[7563b8a]1491 void copyDataToBuffer(const void* srcData, VkBuffer dst, VkDeviceSize dstOffset, VkDeviceSize dataSize) {
1492 VkBuffer stagingBuffer;
1493 VkDeviceMemory stagingBufferMemory;
1494 createBuffer(dataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1495 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1496 stagingBuffer, stagingBufferMemory);
1497
1498 void* data;
1499 vkMapMemory(device, stagingBufferMemory, 0, dataSize, 0, &data);
1500 memcpy(data, srcData, (size_t) dataSize);
1501 vkUnmapMemory(device, stagingBufferMemory);
1502
1503 copyBuffer(stagingBuffer, dst, 0, dstOffset, dataSize);
1504
1505 vkDestroyBuffer(device, stagingBuffer, nullptr);
1506 vkFreeMemory(device, stagingBufferMemory, nullptr);
1507 }
1508
1509 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
1510 VkDeviceSize size) {
[eea05dd]1511 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1512
[7563b8a]1513 VkBufferCopy copyRegion = { srcOffset, dstOffset, size };
[eea05dd]1514 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
1515
1516 endSingleTimeCommands(commandBuffer);
1517 }
1518
1519 VkCommandBuffer beginSingleTimeCommands() {
[d9ef6ab]1520 VkCommandBufferAllocateInfo allocInfo = {};
1521 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1522 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1523 allocInfo.commandPool = commandPool;
1524 allocInfo.commandBufferCount = 1;
1525
1526 VkCommandBuffer commandBuffer;
1527 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
1528
1529 VkCommandBufferBeginInfo beginInfo = {};
1530 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1531 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1532
1533 vkBeginCommandBuffer(commandBuffer, &beginInfo);
1534
[eea05dd]1535 return commandBuffer;
1536 }
[d9ef6ab]1537
[eea05dd]1538 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
[d9ef6ab]1539 vkEndCommandBuffer(commandBuffer);
1540
1541 VkSubmitInfo submitInfo = {};
1542 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1543 submitInfo.commandBufferCount = 1;
1544 submitInfo.pCommandBuffers = &commandBuffer;
1545
1546 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
1547 vkQueueWaitIdle(graphicsQueue);
1548
1549 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
[80edd70]1550 }
1551
[b794178]1552/*** START OF REFACTORED CODE ***/
[80edd70]1553 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
1554 VkPhysicalDeviceMemoryProperties memProperties;
1555 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
1556
1557 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
1558 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
1559 return i;
1560 }
1561 }
1562
1563 throw runtime_error("failed to find suitable memory type!");
1564 }
1565
[721e8be]1566 void createDescriptorPool(GraphicsPipelineInfo& info) {
1567 vector<VkDescriptorPoolSize> poolSizes(info.descriptorInfoList.size());
[c7fb883]1568
[721e8be]1569 for (size_t i = 0; i < poolSizes.size(); i++) {
1570 poolSizes[i].type = info.descriptorInfoList[i].type;
1571 poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
[c7fb883]1572 }
[e5d4aca]1573
1574 VkDescriptorPoolCreateInfo poolInfo = {};
1575 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1576 poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
1577 poolInfo.pPoolSizes = poolSizes.data();
1578 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
1579
1580 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) {
1581 throw runtime_error("failed to create descriptor pool!");
1582 }
1583 }
1584
[721e8be]1585 void createDescriptorSets(GraphicsPipelineInfo& info) {
[e5d4aca]1586 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
1587
1588 VkDescriptorSetAllocateInfo allocInfo = {};
1589 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1590 allocInfo.descriptorPool = info.descriptorPool;
1591 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
1592 allocInfo.pSetLayouts = layouts.data();
1593
1594 info.descriptorSets.resize(swapChainImages.size());
1595 if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) {
1596 throw runtime_error("failed to allocate descriptor sets!");
1597 }
1598
1599 for (size_t i = 0; i < swapChainImages.size(); i++) {
[721e8be]1600 vector<VkWriteDescriptorSet> descriptorWrites(info.descriptorInfoList.size());
1601
1602 for (size_t j = 0; j < descriptorWrites.size(); j++) {
1603 descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1604 descriptorWrites[j].dstSet = info.descriptorSets[i];
1605 descriptorWrites[j].dstBinding = j;
1606 descriptorWrites[j].dstArrayElement = 0;
1607 descriptorWrites[j].descriptorType = info.descriptorInfoList[j].type;
1608 descriptorWrites[j].descriptorCount = 1;
1609 descriptorWrites[j].pBufferInfo = nullptr;
1610 descriptorWrites[j].pImageInfo = nullptr;
1611 descriptorWrites[j].pTexelBufferView = nullptr;
1612
1613 switch (descriptorWrites[j].descriptorType) {
1614 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1615 descriptorWrites[j].pBufferInfo = &(*info.descriptorInfoList[j].bufferDataList)[i];
1616 break;
1617 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1618 descriptorWrites[j].pImageInfo = info.descriptorInfoList[j].imageData;
1619 break;
1620 default:
1621 cout << "Unknown descriptor type: " << descriptorWrites[j].descriptorType << endl;
1622 }
1623 }
[e5d4aca]1624
1625 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
1626 }
1627 }
1628
[47bff4c]1629 void createCommandBuffers() {
1630 commandBuffers.resize(swapChainFramebuffers.size());
1631
1632 VkCommandBufferAllocateInfo allocInfo = {};
1633 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1634 allocInfo.commandPool = commandPool;
1635 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
[621664a]1636 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
[47bff4c]1637
1638 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
[621664a]1639 throw runtime_error("failed to allocate command buffers!");
[47bff4c]1640 }
1641
1642 for (size_t i = 0; i < commandBuffers.size(); i++) {
1643 VkCommandBufferBeginInfo beginInfo = {};
1644 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1645 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
1646 beginInfo.pInheritanceInfo = nullptr;
1647
1648 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
1649 throw runtime_error("failed to begin recording command buffer!");
1650 }
1651
1652 VkRenderPassBeginInfo renderPassInfo = {};
1653 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1654 renderPassInfo.renderPass = renderPass;
1655 renderPassInfo.framebuffer = swapChainFramebuffers[i];
1656 renderPassInfo.renderArea.offset = { 0, 0 };
1657 renderPassInfo.renderArea.extent = swapChainExtent;
1658
[adcd252]1659 array<VkClearValue, 2> clearValues = {};
[f00ee54]1660 clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
[adcd252]1661 clearValues[1].depthStencil = { 1.0f, 0 };
1662
1663 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
1664 renderPassInfo.pClearValues = clearValues.data();
[47bff4c]1665
1666 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
[621664a]1667
[603b5bc]1668/*** END OF REFACTORED CODE ***/
[d22ae72]1669 createGraphicsPipelineCommands(scenePipeline, i);
1670 createGraphicsPipelineCommands(overlayPipeline, i);
[603b5bc]1671/*** START OF REFACTORED CODE ***/
[621664a]1672
[47bff4c]1673 vkCmdEndRenderPass(commandBuffers[i]);
1674
1675 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
1676 throw runtime_error("failed to record command buffer!");
1677 }
1678 }
1679 }
1680
[1187ef5]1681 void createGraphicsPipelineCommands(GraphicsPipelineInfo& info, uint32_t currentImage) {
1682 vkCmdBindPipeline(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
1683 vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
1684 &info.descriptorSets[currentImage], 0, nullptr);
[603b5bc]1685/*** END OF REFACTORED CODE ***/
[d22ae72]1686
1687 VkBuffer vertexBuffers[] = { info.vertexBuffer };
1688 VkDeviceSize offsets[] = { 0 };
[1187ef5]1689 vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets);
[d22ae72]1690
[1187ef5]1691 vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
[d22ae72]1692
[1187ef5]1693 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
[603b5bc]1694/*** START OF REFACTORED CODE ***/
[d22ae72]1695 }
1696
[47bff4c]1697 void createSyncObjects() {
1698 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1699 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1700 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
1701
1702 VkSemaphoreCreateInfo semaphoreInfo = {};
1703 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1704
1705 VkFenceCreateInfo fenceInfo = {};
1706 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1707 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
1708
1709 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1710 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
[1187ef5]1711 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
1712 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
[47bff4c]1713 throw runtime_error("failed to create synchronization objects for a frame!");
1714 }
1715 }
1716 }
[34bdf3a]1717/*** END OF REFACTORED CODE ***/
[47bff4c]1718
[7563b8a]1719 bool addObjectToScene(GraphicsPipelineInfo& info,
1720 const void* vertexData, int vertexSize, size_t numVertices,
1721 vector<uint16_t>& indices, size_t numIndices) {
1722 int indexSize = sizeof(uint16_t);
1723
1724 for (uint16_t& idx : indices) {
1725 idx += info.numVertices;
1726 }
1727
1728 if (info.numVertices + numVertices > info.vertexCapacity) {
1729 cout << "ERROR: Need to resize vertex buffers" << endl;
1730 } else if (info.numIndices + numIndices > info.indexCapacity) {
1731 cout << "ERROR: Need to resize index buffers" << endl;
1732 } else {
1733 cout << "Added object to scene" << endl;
1734
1735 copyDataToBuffer(vertexData, info.vertexBuffer, info.numVertices * vertexSize, numVertices * vertexSize);
1736 info.numVertices += numVertices;
1737
1738 copyDataToBuffer(indices.data(), info.indexBuffer, info.numIndices * indexSize, numIndices * indexSize);
1739 info.numIndices += numIndices;
1740
1741 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
1742 createCommandBuffers();
1743
1744 return true;
1745 }
1746
1747 return false;
1748 }
1749
[c1c2021]1750/*** START OF REFACTORED CODE ***/
[826df16]1751 void mainLoop() {
1752 // TODO: Create some generic event-handling functions in game-gui-*
1753 SDL_Event e;
1754 bool quit = false;
1755
[7dcd925]1756 while (!quit) {
[826df16]1757 while (SDL_PollEvent(&e)) {
1758 if (e.type == SDL_QUIT) {
1759 quit = true;
1760 }
1761 if (e.type == SDL_KEYDOWN) {
[7563b8a]1762 if (e.key.keysym.sym == SDLK_SPACE) {
1763 float zOffset = -0.5f + (0.5f * numPlanes);
1764 vector<Vertex> vertices = {
1765 {{-0.5f, -0.5f, zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
1766 {{ 0.5f, -0.5f, zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
1767 {{ 0.5f, 0.5f, zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
1768 {{-0.5f, 0.5f, zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
1769 };
1770 vector<uint16_t> indices = {
1771 0, 1, 2, 2, 3, 0
1772 };
1773
1774 if (addObjectToScene(scenePipeline,
1775 vertices.data(), sizeof(Vertex), vertices.size(),
1776 indices, indices.size())) {
1777 numPlanes++;
1778 }
1779 } else if (e.key.keysym.sym == SDLK_ESCAPE) {
[91c89f7]1780 quit = true;
1781 }
[826df16]1782 }
1783 if (e.type == SDL_MOUSEBUTTONDOWN) {
1784 quit = true;
1785 }
[75108ef]1786 if (e.type == SDL_WINDOWEVENT) {
[1187ef5]1787 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
1788 e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
[75108ef]1789 framebufferResized = true;
1790 }
1791 }
[47bff4c]1792 }
[321272c]1793
[5f3dba8]1794 drawUI();
[e1a7f5a]1795
1796 drawFrame();
[47bff4c]1797 }
1798
1799 vkDeviceWaitIdle(device);
1800 }
1801
1802 void drawFrame() {
[f94eea9]1803/*** END OF REFACTORED CODE ***/
[47bff4c]1804 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
1805
1806 uint32_t imageIndex;
1807
[621664a]1808 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
1809 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
[75108ef]1810
1811 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
1812 recreateSwapChain();
1813 return;
1814 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
1815 throw runtime_error("failed to acquire swap chain image!");
1816 }
[47bff4c]1817
[de32fda]1818 updateUniformBuffer(imageIndex);
1819
[47bff4c]1820 VkSubmitInfo submitInfo = {};
1821 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1822
1823 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
1824 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
1825
1826 submitInfo.waitSemaphoreCount = 1;
1827 submitInfo.pWaitSemaphores = waitSemaphores;
1828 submitInfo.pWaitDstStageMask = waitStages;
1829 submitInfo.commandBufferCount = 1;
1830 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
1831
1832 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
1833
1834 submitInfo.signalSemaphoreCount = 1;
1835 submitInfo.pSignalSemaphores = signalSemaphores;
1836
[75108ef]1837 vkResetFences(device, 1, &inFlightFences[currentFrame]);
1838
[47bff4c]1839 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
1840 throw runtime_error("failed to submit draw command buffer!");
[bfd620e]1841 }
[47bff4c]1842
1843 VkPresentInfoKHR presentInfo = {};
1844 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1845 presentInfo.waitSemaphoreCount = 1;
1846 presentInfo.pWaitSemaphores = signalSemaphores;
1847
1848 VkSwapchainKHR swapChains[] = { swapChain };
1849 presentInfo.swapchainCount = 1;
1850 presentInfo.pSwapchains = swapChains;
1851 presentInfo.pImageIndices = &imageIndex;
1852 presentInfo.pResults = nullptr;
1853
[75108ef]1854 result = vkQueuePresentKHR(presentQueue, &presentInfo);
1855
1856 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
1857 framebufferResized = false;
1858 recreateSwapChain();
1859 } else if (result != VK_SUCCESS) {
1860 throw runtime_error("failed to present swap chain image!");
1861 }
[47bff4c]1862
1863 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
[fba08f2]1864 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
[f94eea9]1865/*** START OF REFACTORED CODE ***/
[826df16]1866 }
1867
[5f3dba8]1868 void drawUI() {
[f94eea9]1869/*** END OF REFACTORED CODE ***/
[e1a7f5a]1870 // TODO: Since I currently don't use any other render targets,
1871 // I may as well set this once before the render loop
[5f3dba8]1872 SDL_SetRenderTarget(gRenderer, uiOverlay);
1873
[5936c58]1874 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
[5f3dba8]1875 SDL_RenderClear(gRenderer);
1876
1877 SDL_Rect rect;
1878
1879 rect = {280, 220, 100, 100};
[5936c58]1880 SDL_SetRenderDrawColor(gRenderer, 0x00, 0xFF, 0x00, 0xFF);
[5f3dba8]1881 SDL_RenderFillRect(gRenderer, &rect);
1882 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
1883
1884 rect = {10, 10, 0, 0};
1885 SDL_QueryTexture(uiText, nullptr, nullptr, &(rect.w), &(rect.h));
1886 SDL_RenderCopy(gRenderer, uiText, nullptr, &rect);
1887
1888 rect = {10, 80, 0, 0};
1889 SDL_QueryTexture(uiImage, nullptr, nullptr, &(rect.w), &(rect.h));
1890 SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
1891
[5936c58]1892 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
[5f3dba8]1893 SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
1894
[e1a7f5a]1895 populateImageFromSDLTexture(uiOverlay, sdlOverlayImage);
[f94eea9]1896/*** START OF REFACTORED CODE ***/
[5f3dba8]1897 }
[f94eea9]1898/*** END OF REFACTORED CODE ***/
[5f3dba8]1899
[de32fda]1900 void updateUniformBuffer(uint32_t currentImage) {
1901 static auto startTime = chrono::high_resolution_clock::now();
1902
1903 auto currentTime = chrono::high_resolution_clock::now();
1904 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
1905
1906 UniformBufferObject ubo = {};
[f00ee54]1907 ubo.model = rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
1908 ubo.view = 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));
1909 ubo.proj = perspective(radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
[4f63fa8]1910 ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
[fba08f2]1911
[de32fda]1912 void* data;
1913 vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
1914 memcpy(data, &ubo, sizeof(ubo));
1915 vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
1916 }
1917
[621664a]1918 void recreateSwapChain() {
[cabdd5c]1919 gui->refreshWindowSize();
[621664a]1920
[cabdd5c]1921 while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
[621664a]1922 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
1923 SDL_WaitEvent(nullptr);
[cabdd5c]1924 gui->refreshWindowSize();
[621664a]1925 }
1926
1927 vkDeviceWaitIdle(device);
1928
1929 cleanupSwapChain();
1930
1931 createSwapChain();
1932 createImageViews();
1933 createRenderPass();
[d22ae72]1934
[e5d4aca]1935 createBufferResources();
1936 }
[d22ae72]1937
[603b5bc]1938/*** START OF REFACTORED CODE ***/
[e5d4aca]1939 void createBufferResources() {
[adcd252]1940 createDepthResources();
[621664a]1941 createFramebuffers();
1942 createUniformBuffers();
[d22ae72]1943
[e5d4aca]1944 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
[721e8be]1945 createDescriptorPool(scenePipeline);
1946 createDescriptorSets(scenePipeline);
[d22ae72]1947
[e5d4aca]1948 createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline);
[721e8be]1949 createDescriptorPool(overlayPipeline);
1950 createDescriptorSets(overlayPipeline);
[d22ae72]1951
[621664a]1952 createCommandBuffers();
1953 }
1954
[826df16]1955 void cleanup() {
[75108ef]1956 cleanupSwapChain();
1957
[fba08f2]1958 vkDestroySampler(device, textureSampler, nullptr);
[69dccfe]1959
[fba08f2]1960 vkDestroyImageView(device, textureImageView, nullptr);
[eea05dd]1961 vkDestroyImage(device, textureImage, nullptr);
[f5d5686]1962 vkFreeMemory(device, textureImageMemory, nullptr);
[eea05dd]1963
[e1a7f5a]1964 vkDestroyImageView(device, sdlOverlayImageView, nullptr);
1965 vkDestroyImage(device, sdlOverlayImage, nullptr);
1966 vkFreeMemory(device, sdlOverlayImageMemory, nullptr);
1967
[d22ae72]1968 cleanupPipelineBuffers(scenePipeline);
1969 cleanupPipelineBuffers(overlayPipeline);
[80edd70]1970
[47bff4c]1971 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1972 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
[621664a]1973 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
[47bff4c]1974 vkDestroyFence(device, inFlightFences[i], nullptr);
1975 }
1976
[fa9fa1c]1977 vkDestroyCommandPool(device, commandPool, nullptr);
[909b51a]1978 vkDestroyDevice(device, nullptr);
[cabdd5c]1979 vkDestroySurfaceKHR(instance, surface, nullptr);
[909b51a]1980
[80de39d]1981 if (enableValidationLayers) {
1982 DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
1983 }
1984
[826df16]1985 vkDestroyInstance(instance, nullptr);
1986
[5f3dba8]1987 // TODO: Check if any of these functions accept null parameters
1988 // If they do, I don't need to check for that
1989
1990 if (uiOverlay != nullptr) {
1991 SDL_DestroyTexture(uiOverlay);
1992 uiOverlay = nullptr;
1993 }
[b794178]1994/*** END OF REFACTORED CODE ***/
[5f3dba8]1995
1996 TTF_CloseFont(gFont);
1997 gFont = nullptr;
1998
1999 if (uiText != nullptr) {
2000 SDL_DestroyTexture(uiText);
2001 uiText = nullptr;
2002 }
2003
2004 if (uiImage != nullptr) {
2005 SDL_DestroyTexture(uiImage);
2006 uiImage = nullptr;
2007 }
2008
[cabdd5c]2009/*** START OF REFACTORED CODE ***/
[5f3dba8]2010 SDL_DestroyRenderer(gRenderer);
2011 gRenderer = nullptr;
2012
[7fc5e27]2013 gui->destroyWindow();
2014 gui->shutdown();
[98f3232]2015 delete gui;
[826df16]2016 }
[e09ad38]2017
[621664a]2018 void cleanupSwapChain() {
[adcd252]2019 vkDestroyImageView(device, depthImageView, nullptr);
2020 vkDestroyImage(device, depthImage, nullptr);
2021 vkFreeMemory(device, depthImageMemory, nullptr);
2022
[621664a]2023 for (auto framebuffer : swapChainFramebuffers) {
2024 vkDestroyFramebuffer(device, framebuffer, nullptr);
2025 }
2026
2027 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
2028
[d22ae72]2029 cleanupPipeline(scenePipeline);
2030 cleanupPipeline(overlayPipeline);
[b8b32bd]2031
[621664a]2032 vkDestroyRenderPass(device, renderPass, nullptr);
2033
2034 for (auto imageView : swapChainImageViews) {
2035 vkDestroyImageView(device, imageView, nullptr);
2036 }
2037
2038 vkDestroySwapchainKHR(device, swapChain, nullptr);
2039
2040 for (size_t i = 0; i < swapChainImages.size(); i++) {
2041 vkDestroyBuffer(device, uniformBuffers[i], nullptr);
2042 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
2043 }
[d22ae72]2044 }
2045
2046 void cleanupPipeline(GraphicsPipelineInfo& pipeline) {
2047 vkDestroyPipeline(device, pipeline.pipeline, nullptr);
2048 vkDestroyDescriptorPool(device, pipeline.descriptorPool, nullptr);
2049 vkDestroyPipelineLayout(device, pipeline.pipelineLayout, nullptr);
2050 }
2051
2052 void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) {
2053 vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr);
[b794178]2054/*** END OF REFACTORED CODE ***/
[621664a]2055
[d22ae72]2056 vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
2057 vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr);
2058 vkDestroyBuffer(device, pipeline.indexBuffer, nullptr);
2059 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
[621664a]2060 }
2061
[e09ad38]2062 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
[621664a]2063 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
2064 VkDebugUtilsMessageTypeFlagsEXT messageType,
2065 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
2066 void* pUserData) {
[e09ad38]2067 cerr << "validation layer: " << pCallbackData->pMessage << endl;
2068
2069 return VK_FALSE;
[0e6ecf3]2070 }
[e09ad38]2071
2072 static vector<char> readFile(const string& filename) {
2073 ifstream file(filename, ios::ate | ios::binary);
2074
2075 if (!file.is_open()) {
2076 throw runtime_error("failed to open file!");
2077 }
2078
[621664a]2079 size_t fileSize = (size_t) file.tellg();
[e09ad38]2080 vector<char> buffer(fileSize);
2081
2082 file.seekg(0);
2083 file.read(buffer.data(), fileSize);
2084
2085 file.close();
2086
2087 return buffer;
2088 }
[826df16]2089};
2090
[cabdd5c]2091/*** START OF REFACTORED CODE ***/
[1c6cd5e]2092int main(int argc, char* argv[]) {
[826df16]2093
[b6127d2]2094#ifdef NDEBUG
2095 cout << "DEBUGGING IS OFF" << endl;
2096#else
2097 cout << "DEBUGGING IS ON" << endl;
2098#endif
[a8f0577]2099
[826df16]2100 cout << "Starting Vulkan game..." << endl;
2101
2102 VulkanGame game;
2103
2104 try {
2105 game.run();
2106 } catch (const exception& e) {
2107 cerr << e.what() << endl;
2108 return EXIT_FAILURE;
2109 }
[03f4c64]2110
[826df16]2111 cout << "Finished running the game" << endl;
[03f4c64]2112
[826df16]2113 return EXIT_SUCCESS;
[03f4c64]2114}
[cabdd5c]2115/*** END OF REFACTORED CODE ***/
Note: See TracBrowser for help on using the repository browser.