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