[99d44b2] | 1 | #include "vulkan-game.hpp"
|
---|
[850e84c] | 2 |
|
---|
[cc4a8b5] | 3 | #define GLM_FORCE_RADIANS
|
---|
| 4 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
---|
| 5 |
|
---|
| 6 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 7 |
|
---|
[6fc24c7] | 8 | #include <array>
|
---|
[f985231] | 9 | #include <chrono>
|
---|
[0df3c9a] | 10 | #include <iostream>
|
---|
[c1c2021] | 11 | #include <set>
|
---|
[0df3c9a] | 12 |
|
---|
[5edbd58] | 13 | #include "consts.hpp"
|
---|
[c559904] | 14 | #include "logger.hpp"
|
---|
[5edbd58] | 15 |
|
---|
[771b33a] | 16 | #include "utils.hpp"
|
---|
[c1d9b2a] | 17 |
|
---|
[0df3c9a] | 18 | using namespace std;
|
---|
[cc4a8b5] | 19 | using namespace glm;
|
---|
[0df3c9a] | 20 |
|
---|
[b794178] | 21 | struct UniformBufferObject {
|
---|
| 22 | alignas(16) mat4 model;
|
---|
| 23 | alignas(16) mat4 view;
|
---|
| 24 | alignas(16) mat4 proj;
|
---|
| 25 | };
|
---|
| 26 |
|
---|
[34bdf3a] | 27 | VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
|
---|
[0df3c9a] | 28 | gui = nullptr;
|
---|
| 29 | window = nullptr;
|
---|
[1f25a71] | 30 | font = nullptr;
|
---|
| 31 | fontSDLTexture = nullptr;
|
---|
| 32 | imageSDLTexture = nullptr;
|
---|
[87c8f1a] | 33 |
|
---|
| 34 | currentFrame = 0;
|
---|
| 35 | framebufferResized = false;
|
---|
[0df3c9a] | 36 | }
|
---|
| 37 |
|
---|
[99d44b2] | 38 | VulkanGame::~VulkanGame() {
|
---|
[0df3c9a] | 39 | }
|
---|
| 40 |
|
---|
[b6e60b4] | 41 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
[2e77b3f] | 42 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
| 43 |
|
---|
| 44 | cout << "Vulkan Game" << endl;
|
---|
| 45 |
|
---|
[c559904] | 46 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
---|
| 47 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
---|
| 48 | SDL_GetVersion(&sdlVersion);
|
---|
| 49 |
|
---|
| 50 | // TODO: Refactor the logger api to be more flexible,
|
---|
| 51 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
| 52 | restart_gl_log();
|
---|
| 53 | gl_log("starting SDL\n%s.%s.%s",
|
---|
| 54 | to_string(sdlVersion.major).c_str(),
|
---|
| 55 | to_string(sdlVersion.minor).c_str(),
|
---|
| 56 | to_string(sdlVersion.patch).c_str());
|
---|
| 57 |
|
---|
| 58 | open_log();
|
---|
| 59 | get_log() << "starting SDL" << endl;
|
---|
| 60 | get_log() <<
|
---|
| 61 | (int)sdlVersion.major << "." <<
|
---|
| 62 | (int)sdlVersion.minor << "." <<
|
---|
| 63 | (int)sdlVersion.patch << endl;
|
---|
| 64 |
|
---|
[5edbd58] | 65 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
[0df3c9a] | 66 | return;
|
---|
| 67 | }
|
---|
[b6e60b4] | 68 |
|
---|
[0df3c9a] | 69 | initVulkan();
|
---|
| 70 | mainLoop();
|
---|
| 71 | cleanup();
|
---|
[c559904] | 72 |
|
---|
| 73 | close_log();
|
---|
[0df3c9a] | 74 | }
|
---|
| 75 |
|
---|
[0ae182f] | 76 | // TODO: Make some more init functions, or call this initUI if the
|
---|
[c559904] | 77 | // amount of things initialized here keeps growing
|
---|
[b6e60b4] | 78 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
[c559904] | 79 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
[0df3c9a] | 80 | gui = new GameGui_SDL();
|
---|
| 81 |
|
---|
[b6e60b4] | 82 | if (gui->init() == RTWO_ERROR) {
|
---|
[c559904] | 83 | // TODO: Also print these sorts of errors to the log
|
---|
[0df3c9a] | 84 | cout << "UI library could not be initialized!" << endl;
|
---|
[b6e60b4] | 85 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 86 | return RTWO_ERROR;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[b6e60b4] | 89 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
[0df3c9a] | 90 | if (window == nullptr) {
|
---|
| 91 | cout << "Window could not be created!" << endl;
|
---|
[ed7c953] | 92 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 93 | return RTWO_ERROR;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[b6e60b4] | 96 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
[a6f6833] | 97 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
[b6e60b4] | 98 |
|
---|
[c1d9b2a] | 99 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
---|
| 100 | if (renderer == nullptr) {
|
---|
| 101 | cout << "Renderer could not be created!" << endl;
|
---|
| 102 | cout << gui->getError() << endl;
|
---|
| 103 | return RTWO_ERROR;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[b794178] | 106 | SDL_VERSION(&sdlVersion);
|
---|
| 107 |
|
---|
[1f25a71] | 108 | cout << "SDL " << sdlVersion.major << "." << sdlVersion.minor << "." << sdlVersion.patch << endl;
|
---|
| 109 |
|
---|
| 110 | font = TTF_OpenFont("assets/fonts/lazy.ttf", 28);
|
---|
| 111 | if (font == nullptr) {
|
---|
| 112 | cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
| 113 | return RTWO_ERROR;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | SDL_Surface* fontSDLSurface = TTF_RenderText_Solid(font, "Great success!", { 255, 255, 255 });
|
---|
| 117 | if (fontSDLSurface == nullptr) {
|
---|
| 118 | cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
| 119 | return RTWO_ERROR;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | fontSDLTexture = SDL_CreateTextureFromSurface(renderer, fontSDLSurface);
|
---|
| 123 | if (fontSDLTexture == nullptr) {
|
---|
| 124 | cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
|
---|
| 125 | SDL_FreeSurface(fontSDLSurface);
|
---|
| 126 | return RTWO_ERROR;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | SDL_FreeSurface(fontSDLSurface);
|
---|
| 130 |
|
---|
| 131 | // TODO: Load a PNG instead
|
---|
| 132 | SDL_Surface* imageSDLSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
|
---|
| 133 | if (imageSDLSurface == nullptr) {
|
---|
| 134 | cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
|
---|
| 135 | return RTWO_ERROR;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | imageSDLTexture = SDL_CreateTextureFromSurface(renderer, imageSDLSurface);
|
---|
| 139 | if (imageSDLTexture == nullptr) {
|
---|
| 140 | cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
|
---|
| 141 | SDL_FreeSurface(imageSDLSurface);
|
---|
| 142 | return RTWO_ERROR;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | SDL_FreeSurface(imageSDLSurface);
|
---|
| 146 |
|
---|
[b794178] | 147 | // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
|
---|
| 148 | // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
|
---|
| 149 | // 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
|
---|
| 150 | // until the homebrew recipe is updated
|
---|
| 151 | if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
|
---|
| 152 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
| 153 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 154 | } else {
|
---|
| 155 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
---|
| 156 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if (uiOverlay == nullptr) {
|
---|
| 160 | cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
|
---|
[1f25a71] | 161 | return RTWO_ERROR;
|
---|
[b794178] | 162 | }
|
---|
| 163 | if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
|
---|
| 164 | cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
|
---|
[1f25a71] | 165 | return RTWO_ERROR;
|
---|
[b794178] | 166 | }
|
---|
| 167 |
|
---|
[0df3c9a] | 168 | return RTWO_SUCCESS;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[99d44b2] | 171 | void VulkanGame::initVulkan() {
|
---|
[c1d9b2a] | 172 | const vector<const char*> validationLayers = {
|
---|
| 173 | "VK_LAYER_KHRONOS_validation"
|
---|
| 174 | };
|
---|
[fe5c3ba] | 175 | const vector<const char*> deviceExtensions = {
|
---|
| 176 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
| 177 | };
|
---|
[c1d9b2a] | 178 |
|
---|
| 179 | createVulkanInstance(validationLayers);
|
---|
| 180 | setupDebugMessenger();
|
---|
[90a424f] | 181 | createVulkanSurface();
|
---|
[fe5c3ba] | 182 | pickPhysicalDevice(deviceExtensions);
|
---|
[c1c2021] | 183 | createLogicalDevice(validationLayers, deviceExtensions);
|
---|
[502bd0b] | 184 | createSwapChain();
|
---|
[f94eea9] | 185 | createImageViews();
|
---|
[6fc24c7] | 186 | createRenderPass();
|
---|
[fa9fa1c] | 187 | createCommandPool();
|
---|
[7d2b0b9] | 188 |
|
---|
[603b5bc] | 189 | createImageResources();
|
---|
[b794178] | 190 |
|
---|
[603b5bc] | 191 | createFramebuffers();
|
---|
| 192 | createUniformBuffers();
|
---|
| 193 |
|
---|
[87c8f1a] | 194 | vector<Vertex> sceneVertices = {
|
---|
| 195 | {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 196 | {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 197 | {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 198 | {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
|
---|
| 199 |
|
---|
| 200 | {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 201 | {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 202 | {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 203 | {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
| 204 | };
|
---|
| 205 | vector<uint16_t> sceneIndices = {
|
---|
| 206 | 0, 1, 2, 2, 3, 0,
|
---|
| 207 | 4, 5, 6, 6, 7, 4
|
---|
| 208 | };
|
---|
| 209 |
|
---|
| 210 | graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass,
|
---|
[603b5bc] | 211 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(Vertex)));
|
---|
[771b33a] | 212 |
|
---|
[87c8f1a] | 213 | graphicsPipelines.back().bindData(sceneVertices, sceneIndices, commandPool, graphicsQueue);
|
---|
| 214 |
|
---|
[771b33a] | 215 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
|
---|
| 216 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
|
---|
| 217 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
|
---|
| 218 |
|
---|
[b794178] | 219 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
---|
| 220 | VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList);
|
---|
| 221 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
| 222 | VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
|
---|
| 223 |
|
---|
| 224 | graphicsPipelines.back().createDescriptorSetLayout();
|
---|
[7d2b0b9] | 225 | graphicsPipelines.back().createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
|
---|
[b794178] | 226 | graphicsPipelines.back().createDescriptorPool(swapChainImages);
|
---|
| 227 | graphicsPipelines.back().createDescriptorSets(swapChainImages);
|
---|
[7d2b0b9] | 228 |
|
---|
[87c8f1a] | 229 | vector<OverlayVertex> overlayVertices = {
|
---|
| 230 | {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 231 | {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 232 | {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
|
---|
| 233 | {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}
|
---|
| 234 | };
|
---|
| 235 | vector<uint16_t> overlayIndices = {
|
---|
| 236 | 0, 1, 2, 2, 3, 0
|
---|
| 237 | };
|
---|
| 238 |
|
---|
| 239 | graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass,
|
---|
[603b5bc] | 240 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex)));
|
---|
[771b33a] | 241 |
|
---|
[87c8f1a] | 242 | graphicsPipelines.back().bindData(overlayVertices, overlayIndices, commandPool, graphicsQueue);
|
---|
| 243 |
|
---|
[771b33a] | 244 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
|
---|
| 245 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
|
---|
| 246 |
|
---|
[b794178] | 247 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
| 248 | VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
|
---|
| 249 |
|
---|
| 250 | graphicsPipelines.back().createDescriptorSetLayout();
|
---|
[7d2b0b9] | 251 | graphicsPipelines.back().createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
|
---|
[b794178] | 252 | graphicsPipelines.back().createDescriptorPool(swapChainImages);
|
---|
| 253 | graphicsPipelines.back().createDescriptorSets(swapChainImages);
|
---|
[7d2b0b9] | 254 |
|
---|
| 255 | cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl;
|
---|
[34bdf3a] | 256 |
|
---|
[e3bef3a] | 257 | numPlanes = 2;
|
---|
| 258 |
|
---|
[34bdf3a] | 259 | createCommandBuffers();
|
---|
| 260 |
|
---|
| 261 | createSyncObjects();
|
---|
[0df3c9a] | 262 | }
|
---|
| 263 |
|
---|
[99d44b2] | 264 | void VulkanGame::mainLoop() {
|
---|
[f6521fb] | 265 | UIEvent e;
|
---|
[0df3c9a] | 266 | bool quit = false;
|
---|
| 267 |
|
---|
| 268 | while (!quit) {
|
---|
[27c40ce] | 269 | gui->processEvents();
|
---|
| 270 |
|
---|
[f6521fb] | 271 | while (gui->pollEvent(&e)) {
|
---|
| 272 | switch(e.type) {
|
---|
| 273 | case UI_EVENT_QUIT:
|
---|
| 274 | cout << "Quit event detected" << endl;
|
---|
| 275 | quit = true;
|
---|
| 276 | break;
|
---|
| 277 | case UI_EVENT_WINDOW:
|
---|
| 278 | cout << "Window event detected" << endl;
|
---|
| 279 | // Currently unused
|
---|
| 280 | break;
|
---|
[0e09340] | 281 | case UI_EVENT_WINDOWRESIZE:
|
---|
| 282 | cout << "Window resize event detected" << endl;
|
---|
| 283 | framebufferResized = true;
|
---|
| 284 | break;
|
---|
[e3bef3a] | 285 | case UI_EVENT_KEYDOWN:
|
---|
[f6521fb] | 286 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
| 287 | quit = true;
|
---|
[e3bef3a] | 288 | } else if (e.key.keycode == SDL_SCANCODE_SPACE) {
|
---|
| 289 | cout << "Adding a plane" << endl;
|
---|
| 290 | float zOffset = -0.5f + (0.5f * numPlanes);
|
---|
| 291 | vector<Vertex> vertices = {
|
---|
| 292 | {{-0.5f, -0.5f, zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 293 | {{ 0.5f, -0.5f, zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 294 | {{ 0.5f, 0.5f, zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 295 | {{-0.5f, 0.5f, zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
| 296 | };
|
---|
| 297 | vector<uint16_t> indices = {
|
---|
| 298 | 0, 1, 2, 2, 3, 0
|
---|
| 299 | };
|
---|
| 300 |
|
---|
| 301 | if (graphicsPipelines[0].addObject(vertices, indices, commandPool, graphicsQueue)) {
|
---|
| 302 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 303 | createCommandBuffers();
|
---|
| 304 |
|
---|
| 305 | numPlanes++;
|
---|
| 306 | }
|
---|
[f6521fb] | 307 | } else {
|
---|
| 308 | cout << "Key event detected" << endl;
|
---|
| 309 | }
|
---|
| 310 | break;
|
---|
| 311 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
| 312 | cout << "Mouse button down event detected" << endl;
|
---|
| 313 | break;
|
---|
| 314 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
| 315 | cout << "Mouse button up event detected" << endl;
|
---|
| 316 | break;
|
---|
| 317 | case UI_EVENT_MOUSEMOTION:
|
---|
| 318 | break;
|
---|
[a0da009] | 319 | case UI_EVENT_UNKNOWN:
|
---|
| 320 | cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;
|
---|
| 321 | break;
|
---|
[c61323a] | 322 | default:
|
---|
| 323 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
[0df3c9a] | 324 | }
|
---|
| 325 | }
|
---|
[c1d9b2a] | 326 |
|
---|
[a0c5f28] | 327 | renderUI();
|
---|
| 328 | renderScene();
|
---|
[0df3c9a] | 329 | }
|
---|
[c1c2021] | 330 |
|
---|
| 331 | vkDeviceWaitIdle(device);
|
---|
[0df3c9a] | 332 | }
|
---|
| 333 |
|
---|
[a0c5f28] | 334 | void VulkanGame::renderUI() {
|
---|
[d2d9286] | 335 | // TODO: Since I currently don't use any other render targets,
|
---|
| 336 | // I may as well set this once before the render loop
|
---|
| 337 | SDL_SetRenderTarget(renderer, uiOverlay);
|
---|
| 338 |
|
---|
| 339 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
---|
[a0c5f28] | 340 | SDL_RenderClear(renderer);
|
---|
[d2d9286] | 341 |
|
---|
| 342 | SDL_Rect rect = {280, 220, 100, 100};
|
---|
| 343 | SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
---|
| 344 | SDL_RenderFillRect(renderer, &rect);
|
---|
| 345 |
|
---|
[1f25a71] | 346 | rect = {10, 10, 0, 0};
|
---|
| 347 | SDL_QueryTexture(fontSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
| 348 | SDL_RenderCopy(renderer, fontSDLTexture, nullptr, &rect);
|
---|
| 349 |
|
---|
| 350 | rect = {10, 80, 0, 0};
|
---|
| 351 | SDL_QueryTexture(imageSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
| 352 | SDL_RenderCopy(renderer, imageSDLTexture, nullptr, &rect);
|
---|
| 353 |
|
---|
[d2d9286] | 354 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
|
---|
| 355 | SDL_RenderDrawLine(renderer, 50, 5, 150, 500);
|
---|
| 356 |
|
---|
| 357 | VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer,
|
---|
| 358 | sdlOverlayImage, graphicsQueue);
|
---|
[a0c5f28] | 359 | }
|
---|
| 360 |
|
---|
| 361 | void VulkanGame::renderScene() {
|
---|
[87c8f1a] | 362 | vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
|
---|
| 363 |
|
---|
| 364 | uint32_t imageIndex;
|
---|
| 365 |
|
---|
[d2d9286] | 366 | VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
|
---|
| 367 | imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
---|
| 368 |
|
---|
| 369 | if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
---|
| 370 | recreateSwapChain();
|
---|
| 371 | return;
|
---|
| 372 | } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
|
---|
| 373 | throw runtime_error("failed to acquire swap chain image!");
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[f985231] | 376 | updateUniformBuffer(imageIndex);
|
---|
| 377 |
|
---|
[d2d9286] | 378 | VkSubmitInfo submitInfo = {};
|
---|
| 379 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
---|
| 380 |
|
---|
| 381 | VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
|
---|
| 382 | VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
---|
| 383 |
|
---|
| 384 | submitInfo.waitSemaphoreCount = 1;
|
---|
| 385 | submitInfo.pWaitSemaphores = waitSemaphores;
|
---|
| 386 | submitInfo.pWaitDstStageMask = waitStages;
|
---|
| 387 | submitInfo.commandBufferCount = 1;
|
---|
| 388 | submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
|
---|
| 389 |
|
---|
| 390 | VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
|
---|
| 391 |
|
---|
| 392 | submitInfo.signalSemaphoreCount = 1;
|
---|
| 393 | submitInfo.pSignalSemaphores = signalSemaphores;
|
---|
| 394 |
|
---|
| 395 | vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
---|
| 396 |
|
---|
| 397 | if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
|
---|
| 398 | throw runtime_error("failed to submit draw command buffer!");
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | VkPresentInfoKHR presentInfo = {};
|
---|
| 402 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
---|
| 403 | presentInfo.waitSemaphoreCount = 1;
|
---|
| 404 | presentInfo.pWaitSemaphores = signalSemaphores;
|
---|
| 405 |
|
---|
| 406 | VkSwapchainKHR swapChains[] = { swapChain };
|
---|
| 407 | presentInfo.swapchainCount = 1;
|
---|
| 408 | presentInfo.pSwapchains = swapChains;
|
---|
| 409 | presentInfo.pImageIndices = &imageIndex;
|
---|
| 410 | presentInfo.pResults = nullptr;
|
---|
| 411 |
|
---|
| 412 | result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
---|
| 413 |
|
---|
| 414 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
|
---|
| 415 | framebufferResized = false;
|
---|
| 416 | recreateSwapChain();
|
---|
| 417 | } else if (result != VK_SUCCESS) {
|
---|
| 418 | throw runtime_error("failed to present swap chain image!");
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[87c8f1a] | 421 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
| 422 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
[a0c5f28] | 423 | }
|
---|
| 424 |
|
---|
[99d44b2] | 425 | void VulkanGame::cleanup() {
|
---|
[c1c2021] | 426 | cleanupSwapChain();
|
---|
| 427 |
|
---|
[e83b155] | 428 | VulkanUtils::destroyVulkanImage(device, floorTextureImage);
|
---|
| 429 | VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);
|
---|
| 430 |
|
---|
| 431 | vkDestroySampler(device, textureSampler, nullptr);
|
---|
| 432 |
|
---|
[b794178] | 433 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
| 434 | pipeline.cleanupBuffers();
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[34bdf3a] | 437 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
| 438 | vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
---|
| 439 | vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
---|
| 440 | vkDestroyFence(device, inFlightFences[i], nullptr);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[fa9fa1c] | 443 | vkDestroyCommandPool(device, commandPool, nullptr);
|
---|
[c1c2021] | 444 | vkDestroyDevice(device, nullptr);
|
---|
| 445 | vkDestroySurfaceKHR(instance, surface, nullptr);
|
---|
| 446 |
|
---|
[c1d9b2a] | 447 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 448 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
| 449 | }
|
---|
[c1c2021] | 450 |
|
---|
[c1d9b2a] | 451 | vkDestroyInstance(instance, nullptr);
|
---|
| 452 |
|
---|
[b794178] | 453 | // TODO: Check if any of these functions accept null parameters
|
---|
| 454 | // If they do, I don't need to check for that
|
---|
| 455 |
|
---|
| 456 | if (uiOverlay != nullptr) {
|
---|
| 457 | SDL_DestroyTexture(uiOverlay);
|
---|
| 458 | uiOverlay = nullptr;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
[1f25a71] | 461 | if (fontSDLTexture != nullptr) {
|
---|
| 462 | SDL_DestroyTexture(fontSDLTexture);
|
---|
| 463 | fontSDLTexture = nullptr;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | if (imageSDLTexture != nullptr) {
|
---|
| 467 | SDL_DestroyTexture(imageSDLTexture);
|
---|
| 468 | imageSDLTexture = nullptr;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | TTF_CloseFont(font);
|
---|
| 472 | font = nullptr;
|
---|
| 473 |
|
---|
[c1d9b2a] | 474 | SDL_DestroyRenderer(renderer);
|
---|
| 475 | renderer = nullptr;
|
---|
| 476 |
|
---|
[b6e60b4] | 477 | gui->destroyWindow();
|
---|
| 478 | gui->shutdown();
|
---|
[0df3c9a] | 479 | delete gui;
|
---|
[c1d9b2a] | 480 | }
|
---|
| 481 |
|
---|
| 482 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
---|
| 483 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
---|
| 484 | throw runtime_error("validation layers requested, but not available!");
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | VkApplicationInfo appInfo = {};
|
---|
| 488 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
| 489 | appInfo.pApplicationName = "Vulkan Game";
|
---|
| 490 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
| 491 | appInfo.pEngineName = "No Engine";
|
---|
| 492 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
| 493 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
| 494 |
|
---|
| 495 | VkInstanceCreateInfo createInfo = {};
|
---|
| 496 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
| 497 | createInfo.pApplicationInfo = &appInfo;
|
---|
| 498 |
|
---|
| 499 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
| 500 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 501 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
| 505 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
| 506 |
|
---|
| 507 | cout << endl << "Extensions:" << endl;
|
---|
| 508 | for (const char* extensionName : extensions) {
|
---|
| 509 | cout << extensionName << endl;
|
---|
| 510 | }
|
---|
| 511 | cout << endl;
|
---|
| 512 |
|
---|
| 513 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
| 514 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 515 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
| 516 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
| 517 |
|
---|
| 518 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
| 519 | createInfo.pNext = &debugCreateInfo;
|
---|
| 520 | } else {
|
---|
| 521 | createInfo.enabledLayerCount = 0;
|
---|
| 522 |
|
---|
| 523 | createInfo.pNext = nullptr;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
| 527 | throw runtime_error("failed to create instance!");
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | void VulkanGame::setupDebugMessenger() {
|
---|
| 532 | if (!ENABLE_VALIDATION_LAYERS) return;
|
---|
| 533 |
|
---|
| 534 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
| 535 | populateDebugMessengerCreateInfo(createInfo);
|
---|
| 536 |
|
---|
| 537 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
| 538 | throw runtime_error("failed to set up debug messenger!");
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
| 543 | createInfo = {};
|
---|
| 544 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
| 545 | 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;
|
---|
| 546 | 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;
|
---|
| 547 | createInfo.pfnUserCallback = debugCallback;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
---|
| 551 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 552 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 553 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 554 | void* pUserData) {
|
---|
| 555 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
| 556 |
|
---|
| 557 | return VK_FALSE;
|
---|
| 558 | }
|
---|
[90a424f] | 559 |
|
---|
| 560 | void VulkanGame::createVulkanSurface() {
|
---|
| 561 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
| 562 | throw runtime_error("failed to create window surface!");
|
---|
| 563 | }
|
---|
| 564 | }
|
---|
| 565 |
|
---|
[fe5c3ba] | 566 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 567 | uint32_t deviceCount = 0;
|
---|
| 568 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
| 569 |
|
---|
| 570 | if (deviceCount == 0) {
|
---|
| 571 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
| 575 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
| 576 |
|
---|
| 577 | cout << endl << "Graphics cards:" << endl;
|
---|
| 578 | for (const VkPhysicalDevice& device : devices) {
|
---|
[fe5c3ba] | 579 | if (isDeviceSuitable(device, deviceExtensions)) {
|
---|
[90a424f] | 580 | physicalDevice = device;
|
---|
| 581 | break;
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 | cout << endl;
|
---|
| 585 |
|
---|
| 586 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
| 587 | throw runtime_error("failed to find a suitable GPU!");
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[fa9fa1c] | 591 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice physicalDevice,
|
---|
| 592 | const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 593 | VkPhysicalDeviceProperties deviceProperties;
|
---|
[fa9fa1c] | 594 | vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
|
---|
[90a424f] | 595 |
|
---|
| 596 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
| 597 |
|
---|
[fa9fa1c] | 598 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 599 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(physicalDevice, deviceExtensions);
|
---|
[90a424f] | 600 | bool swapChainAdequate = false;
|
---|
| 601 |
|
---|
| 602 | if (extensionsSupported) {
|
---|
[fa9fa1c] | 603 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
[90a424f] | 604 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
[fa9fa1c] | 608 | vkGetPhysicalDeviceFeatures(physicalDevice, &supportedFeatures);
|
---|
[90a424f] | 609 |
|
---|
| 610 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
[c1c2021] | 611 | }
|
---|
| 612 |
|
---|
| 613 | void VulkanGame::createLogicalDevice(
|
---|
| 614 | const vector<const char*> validationLayers,
|
---|
| 615 | const vector<const char*>& deviceExtensions) {
|
---|
| 616 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 617 |
|
---|
[b794178] | 618 | vector<VkDeviceQueueCreateInfo> queueCreateInfoList;
|
---|
[c1c2021] | 619 | set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
| 620 |
|
---|
| 621 | float queuePriority = 1.0f;
|
---|
| 622 | for (uint32_t queueFamily : uniqueQueueFamilies) {
|
---|
| 623 | VkDeviceQueueCreateInfo queueCreateInfo = {};
|
---|
| 624 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
---|
| 625 | queueCreateInfo.queueFamilyIndex = queueFamily;
|
---|
| 626 | queueCreateInfo.queueCount = 1;
|
---|
| 627 | queueCreateInfo.pQueuePriorities = &queuePriority;
|
---|
| 628 |
|
---|
[b794178] | 629 | queueCreateInfoList.push_back(queueCreateInfo);
|
---|
[c1c2021] | 630 | }
|
---|
| 631 |
|
---|
| 632 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
---|
| 633 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
---|
| 634 |
|
---|
| 635 | VkDeviceCreateInfo createInfo = {};
|
---|
| 636 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
---|
[b794178] | 637 | createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfoList.size());
|
---|
| 638 | createInfo.pQueueCreateInfos = queueCreateInfoList.data();
|
---|
[c1c2021] | 639 |
|
---|
| 640 | createInfo.pEnabledFeatures = &deviceFeatures;
|
---|
| 641 |
|
---|
| 642 | createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
---|
| 643 | createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
---|
| 644 |
|
---|
| 645 | // These fields are ignored by up-to-date Vulkan implementations,
|
---|
| 646 | // but it's a good idea to set them for backwards compatibility
|
---|
| 647 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 648 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
| 649 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
| 650 | } else {
|
---|
| 651 | createInfo.enabledLayerCount = 0;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
---|
| 655 | throw runtime_error("failed to create logical device!");
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
---|
| 659 | vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
---|
[502bd0b] | 660 | }
|
---|
| 661 |
|
---|
| 662 | void VulkanGame::createSwapChain() {
|
---|
| 663 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
| 664 |
|
---|
| 665 | VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
|
---|
| 666 | VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
|
---|
| 667 | VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 668 |
|
---|
| 669 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
---|
| 670 | if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
|
---|
| 671 | imageCount = swapChainSupport.capabilities.maxImageCount;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | VkSwapchainCreateInfoKHR createInfo = {};
|
---|
| 675 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
---|
| 676 | createInfo.surface = surface;
|
---|
| 677 | createInfo.minImageCount = imageCount;
|
---|
| 678 | createInfo.imageFormat = surfaceFormat.format;
|
---|
| 679 | createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
---|
| 680 | createInfo.imageExtent = extent;
|
---|
| 681 | createInfo.imageArrayLayers = 1;
|
---|
| 682 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
---|
| 683 |
|
---|
| 684 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 685 | uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
| 686 |
|
---|
| 687 | if (indices.graphicsFamily != indices.presentFamily) {
|
---|
| 688 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
---|
| 689 | createInfo.queueFamilyIndexCount = 2;
|
---|
| 690 | createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
---|
[f94eea9] | 691 | } else {
|
---|
[502bd0b] | 692 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
| 693 | createInfo.queueFamilyIndexCount = 0;
|
---|
| 694 | createInfo.pQueueFamilyIndices = nullptr;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
---|
| 698 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
---|
| 699 | createInfo.presentMode = presentMode;
|
---|
| 700 | createInfo.clipped = VK_TRUE;
|
---|
| 701 | createInfo.oldSwapchain = VK_NULL_HANDLE;
|
---|
| 702 |
|
---|
| 703 | if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
|
---|
| 704 | throw runtime_error("failed to create swap chain!");
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
---|
| 708 | swapChainImages.resize(imageCount);
|
---|
| 709 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
---|
| 710 |
|
---|
| 711 | swapChainImageFormat = surfaceFormat.format;
|
---|
[603b5bc] | 712 | swapChainExtent = extent;
|
---|
[f94eea9] | 713 | }
|
---|
| 714 |
|
---|
| 715 | void VulkanGame::createImageViews() {
|
---|
| 716 | swapChainImageViews.resize(swapChainImages.size());
|
---|
| 717 |
|
---|
| 718 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
| 719 | swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainImageFormat,
|
---|
| 720 | VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
| 721 | }
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[6fc24c7] | 724 | void VulkanGame::createRenderPass() {
|
---|
| 725 | VkAttachmentDescription colorAttachment = {};
|
---|
| 726 | colorAttachment.format = swapChainImageFormat;
|
---|
| 727 | colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 728 | colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
| 729 | colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
---|
| 730 | colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
| 731 | colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 732 | colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
| 733 | colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
---|
| 734 |
|
---|
| 735 | VkAttachmentReference colorAttachmentRef = {};
|
---|
| 736 | colorAttachmentRef.attachment = 0;
|
---|
| 737 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
---|
| 738 |
|
---|
| 739 | VkAttachmentDescription depthAttachment = {};
|
---|
| 740 | depthAttachment.format = findDepthFormat();
|
---|
| 741 | depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 742 | depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
| 743 | depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 744 | depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
| 745 | depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 746 | depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
| 747 | depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
| 748 |
|
---|
| 749 | VkAttachmentReference depthAttachmentRef = {};
|
---|
| 750 | depthAttachmentRef.attachment = 1;
|
---|
| 751 | depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
| 752 |
|
---|
| 753 | VkSubpassDescription subpass = {};
|
---|
| 754 | subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
---|
| 755 | subpass.colorAttachmentCount = 1;
|
---|
| 756 | subpass.pColorAttachments = &colorAttachmentRef;
|
---|
| 757 | subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
---|
| 758 |
|
---|
| 759 | VkSubpassDependency dependency = {};
|
---|
| 760 | dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
---|
| 761 | dependency.dstSubpass = 0;
|
---|
| 762 | dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
| 763 | dependency.srcAccessMask = 0;
|
---|
| 764 | dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
| 765 | dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
---|
| 766 |
|
---|
| 767 | array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
|
---|
| 768 | VkRenderPassCreateInfo renderPassInfo = {};
|
---|
| 769 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
---|
| 770 | renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
| 771 | renderPassInfo.pAttachments = attachments.data();
|
---|
| 772 | renderPassInfo.subpassCount = 1;
|
---|
| 773 | renderPassInfo.pSubpasses = &subpass;
|
---|
| 774 | renderPassInfo.dependencyCount = 1;
|
---|
| 775 | renderPassInfo.pDependencies = &dependency;
|
---|
| 776 |
|
---|
| 777 | if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
|
---|
| 778 | throw runtime_error("failed to create render pass!");
|
---|
| 779 | }
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | VkFormat VulkanGame::findDepthFormat() {
|
---|
| 783 | return VulkanUtils::findSupportedFormat(
|
---|
| 784 | physicalDevice,
|
---|
| 785 | { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
|
---|
| 786 | VK_IMAGE_TILING_OPTIMAL,
|
---|
| 787 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
|
---|
| 788 | );
|
---|
| 789 | }
|
---|
| 790 |
|
---|
[fa9fa1c] | 791 | void VulkanGame::createCommandPool() {
|
---|
| 792 | QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);;
|
---|
| 793 |
|
---|
| 794 | VkCommandPoolCreateInfo poolInfo = {};
|
---|
| 795 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
---|
| 796 | poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
|
---|
| 797 | poolInfo.flags = 0;
|
---|
| 798 |
|
---|
| 799 | if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
|
---|
| 800 | throw runtime_error("failed to create graphics command pool!");
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
| 803 |
|
---|
[603b5bc] | 804 | void VulkanGame::createImageResources() {
|
---|
| 805 | VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
|
---|
| 806 | depthImage, graphicsQueue);
|
---|
[b794178] | 807 |
|
---|
[603b5bc] | 808 | createTextureSampler();
|
---|
[b794178] | 809 |
|
---|
| 810 | VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
|
---|
| 811 | floorTextureImage, graphicsQueue);
|
---|
| 812 |
|
---|
| 813 | floorTextureImageDescriptor = {};
|
---|
| 814 | floorTextureImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
| 815 | floorTextureImageDescriptor.imageView = floorTextureImage.imageView;
|
---|
| 816 | floorTextureImageDescriptor.sampler = textureSampler;
|
---|
| 817 |
|
---|
[603b5bc] | 818 | VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
|
---|
| 819 |
|
---|
[b794178] | 820 | sdlOverlayImageDescriptor = {};
|
---|
| 821 | sdlOverlayImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
| 822 | sdlOverlayImageDescriptor.imageView = sdlOverlayImage.imageView;
|
---|
| 823 | sdlOverlayImageDescriptor.sampler = textureSampler;
|
---|
| 824 | }
|
---|
| 825 |
|
---|
| 826 | void VulkanGame::createTextureSampler() {
|
---|
| 827 | VkSamplerCreateInfo samplerInfo = {};
|
---|
| 828 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
---|
| 829 | samplerInfo.magFilter = VK_FILTER_LINEAR;
|
---|
| 830 | samplerInfo.minFilter = VK_FILTER_LINEAR;
|
---|
| 831 |
|
---|
| 832 | samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 833 | samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 834 | samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 835 |
|
---|
| 836 | samplerInfo.anisotropyEnable = VK_TRUE;
|
---|
| 837 | samplerInfo.maxAnisotropy = 16;
|
---|
| 838 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
---|
| 839 | samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
---|
| 840 | samplerInfo.compareEnable = VK_FALSE;
|
---|
| 841 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
---|
| 842 | samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
---|
| 843 | samplerInfo.mipLodBias = 0.0f;
|
---|
| 844 | samplerInfo.minLod = 0.0f;
|
---|
| 845 | samplerInfo.maxLod = 0.0f;
|
---|
| 846 |
|
---|
| 847 | if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
|
---|
| 848 | throw runtime_error("failed to create texture sampler!");
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 |
|
---|
[603b5bc] | 852 | void VulkanGame::createFramebuffers() {
|
---|
| 853 | swapChainFramebuffers.resize(swapChainImageViews.size());
|
---|
| 854 |
|
---|
| 855 | for (size_t i = 0; i < swapChainImageViews.size(); i++) {
|
---|
| 856 | array<VkImageView, 2> attachments = {
|
---|
| 857 | swapChainImageViews[i],
|
---|
| 858 | depthImage.imageView
|
---|
| 859 | };
|
---|
| 860 |
|
---|
| 861 | VkFramebufferCreateInfo framebufferInfo = {};
|
---|
| 862 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
---|
| 863 | framebufferInfo.renderPass = renderPass;
|
---|
| 864 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
| 865 | framebufferInfo.pAttachments = attachments.data();
|
---|
| 866 | framebufferInfo.width = swapChainExtent.width;
|
---|
| 867 | framebufferInfo.height = swapChainExtent.height;
|
---|
| 868 | framebufferInfo.layers = 1;
|
---|
| 869 |
|
---|
| 870 | if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
|
---|
| 871 | throw runtime_error("failed to create framebuffer!");
|
---|
| 872 | }
|
---|
| 873 | }
|
---|
| 874 | }
|
---|
| 875 |
|
---|
[b794178] | 876 | void VulkanGame::createUniformBuffers() {
|
---|
| 877 | VkDeviceSize bufferSize = sizeof(UniformBufferObject);
|
---|
| 878 |
|
---|
| 879 | uniformBuffers.resize(swapChainImages.size());
|
---|
| 880 | uniformBuffersMemory.resize(swapChainImages.size());
|
---|
| 881 | uniformBufferInfoList.resize(swapChainImages.size());
|
---|
| 882 |
|
---|
| 883 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
| 884 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
| 885 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 886 | uniformBuffers[i], uniformBuffersMemory[i]);
|
---|
| 887 |
|
---|
| 888 | uniformBufferInfoList[i].buffer = uniformBuffers[i];
|
---|
| 889 | uniformBufferInfoList[i].offset = 0;
|
---|
| 890 | uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
|
---|
| 891 | }
|
---|
| 892 | }
|
---|
| 893 |
|
---|
[603b5bc] | 894 | void VulkanGame::createCommandBuffers() {
|
---|
| 895 | commandBuffers.resize(swapChainImages.size());
|
---|
| 896 |
|
---|
| 897 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
| 898 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
| 899 | allocInfo.commandPool = commandPool;
|
---|
| 900 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
| 901 | allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
|
---|
| 902 |
|
---|
| 903 | if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
|
---|
| 904 | throw runtime_error("failed to allocate command buffers!");
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | for (size_t i = 0; i < commandBuffers.size(); i++) {
|
---|
| 908 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
| 909 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
| 910 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
---|
| 911 | beginInfo.pInheritanceInfo = nullptr;
|
---|
| 912 |
|
---|
| 913 | if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
|
---|
| 914 | throw runtime_error("failed to begin recording command buffer!");
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | VkRenderPassBeginInfo renderPassInfo = {};
|
---|
| 918 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
---|
| 919 | renderPassInfo.renderPass = renderPass;
|
---|
| 920 | renderPassInfo.framebuffer = swapChainFramebuffers[i];
|
---|
| 921 | renderPassInfo.renderArea.offset = { 0, 0 };
|
---|
| 922 | renderPassInfo.renderArea.extent = swapChainExtent;
|
---|
| 923 |
|
---|
| 924 | array<VkClearValue, 2> clearValues = {};
|
---|
| 925 | clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
|
---|
| 926 | clearValues[1].depthStencil = { 1.0f, 0 };
|
---|
| 927 |
|
---|
| 928 | renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
---|
| 929 | renderPassInfo.pClearValues = clearValues.data();
|
---|
| 930 |
|
---|
| 931 | vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
---|
| 932 |
|
---|
| 933 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
| 934 | pipeline.createRenderCommands(commandBuffers[i], i);
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | vkCmdEndRenderPass(commandBuffers[i]);
|
---|
| 938 |
|
---|
| 939 | if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
|
---|
| 940 | throw runtime_error("failed to record command buffer!");
|
---|
| 941 | }
|
---|
| 942 | }
|
---|
| 943 | }
|
---|
| 944 |
|
---|
[34bdf3a] | 945 | void VulkanGame::createSyncObjects() {
|
---|
| 946 | imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 947 | renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 948 | inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 949 |
|
---|
| 950 | VkSemaphoreCreateInfo semaphoreInfo = {};
|
---|
| 951 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
---|
| 952 |
|
---|
| 953 | VkFenceCreateInfo fenceInfo = {};
|
---|
| 954 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
---|
| 955 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
---|
| 956 |
|
---|
| 957 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
| 958 | if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
|
---|
| 959 | vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
|
---|
| 960 | vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
|
---|
| 961 | throw runtime_error("failed to create synchronization objects for a frame!");
|
---|
| 962 | }
|
---|
| 963 | }
|
---|
| 964 | }
|
---|
| 965 |
|
---|
[e3bef3a] | 966 | // TODO: Fix the crash that happens when alt-tabbing
|
---|
[d2d9286] | 967 | void VulkanGame::recreateSwapChain() {
|
---|
| 968 | cout << "Recreating swap chain" << endl;
|
---|
| 969 | gui->refreshWindowSize();
|
---|
| 970 |
|
---|
| 971 | while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
|
---|
| 972 | (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
|
---|
| 973 | SDL_WaitEvent(nullptr);
|
---|
| 974 | gui->refreshWindowSize();
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | vkDeviceWaitIdle(device);
|
---|
| 978 |
|
---|
[0ae182f] | 979 | cleanupSwapChain();
|
---|
| 980 |
|
---|
| 981 | createSwapChain();
|
---|
| 982 | createImageViews();
|
---|
| 983 | createRenderPass();
|
---|
| 984 |
|
---|
| 985 | VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
|
---|
| 986 | depthImage, graphicsQueue);
|
---|
| 987 | createFramebuffers();
|
---|
| 988 | createUniformBuffers();
|
---|
| 989 |
|
---|
| 990 | graphicsPipelines[0].updateRenderPass(renderPass);
|
---|
| 991 | graphicsPipelines[0].createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
|
---|
| 992 | graphicsPipelines[0].createDescriptorPool(swapChainImages);
|
---|
| 993 | graphicsPipelines[0].createDescriptorSets(swapChainImages);
|
---|
| 994 |
|
---|
| 995 | graphicsPipelines[1].updateRenderPass(renderPass);
|
---|
| 996 | graphicsPipelines[1].createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
|
---|
| 997 | graphicsPipelines[1].createDescriptorPool(swapChainImages);
|
---|
| 998 | graphicsPipelines[1].createDescriptorSets(swapChainImages);
|
---|
| 999 |
|
---|
| 1000 | createCommandBuffers();
|
---|
[d2d9286] | 1001 | }
|
---|
| 1002 |
|
---|
[f985231] | 1003 | void VulkanGame::updateUniformBuffer(uint32_t currentImage) {
|
---|
| 1004 | static auto startTime = chrono::high_resolution_clock::now();
|
---|
| 1005 |
|
---|
| 1006 | auto currentTime = chrono::high_resolution_clock::now();
|
---|
| 1007 | float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
|
---|
| 1008 |
|
---|
| 1009 | UniformBufferObject ubo = {};
|
---|
[cc4a8b5] | 1010 | ubo.model = rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
|
---|
| 1011 | ubo.view = lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
|
---|
[f985231] | 1012 | ubo.proj = perspective(radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
|
---|
| 1013 | ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
|
---|
| 1014 |
|
---|
| 1015 | void* data;
|
---|
| 1016 | vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
|
---|
| 1017 | memcpy(data, &ubo, sizeof(ubo));
|
---|
| 1018 | vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
[f94eea9] | 1021 | void VulkanGame::cleanupSwapChain() {
|
---|
[603b5bc] | 1022 | VulkanUtils::destroyVulkanImage(device, depthImage);
|
---|
| 1023 |
|
---|
| 1024 | for (VkFramebuffer framebuffer : swapChainFramebuffers) {
|
---|
| 1025 | vkDestroyFramebuffer(device, framebuffer, nullptr);
|
---|
| 1026 | }
|
---|
| 1027 |
|
---|
| 1028 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 1029 |
|
---|
[b794178] | 1030 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
| 1031 | pipeline.cleanup();
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
[6fc24c7] | 1034 | vkDestroyRenderPass(device, renderPass, nullptr);
|
---|
| 1035 |
|
---|
[b794178] | 1036 | for (VkImageView imageView : swapChainImageViews) {
|
---|
[f94eea9] | 1037 | vkDestroyImageView(device, imageView, nullptr);
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | vkDestroySwapchainKHR(device, swapChain, nullptr);
|
---|
[e83b155] | 1041 |
|
---|
[603b5bc] | 1042 | for (size_t i = 0; i < uniformBuffers.size(); i++) {
|
---|
[e83b155] | 1043 | vkDestroyBuffer(device, uniformBuffers[i], nullptr);
|
---|
| 1044 | vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
|
---|
| 1045 | }
|
---|
[cc4a8b5] | 1046 | }
|
---|