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