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