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