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