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