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