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