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