[99d44b2] | 1 | #include "vulkan-game.hpp"
|
---|
[850e84c] | 2 |
|
---|
[6fc24c7] | 3 | #include <array>
|
---|
[f985231] | 4 | #include <chrono>
|
---|
[0df3c9a] | 5 | #include <iostream>
|
---|
[c1c2021] | 6 | #include <set>
|
---|
[0df3c9a] | 7 |
|
---|
[5edbd58] | 8 | #include "consts.hpp"
|
---|
[c559904] | 9 | #include "logger.hpp"
|
---|
[5edbd58] | 10 |
|
---|
[771b33a] | 11 | #include "utils.hpp"
|
---|
[c1d9b2a] | 12 |
|
---|
[0df3c9a] | 13 | using namespace std;
|
---|
[b794178] | 14 |
|
---|
[34bdf3a] | 15 | VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
|
---|
[0df3c9a] | 16 | gui = nullptr;
|
---|
| 17 | window = nullptr;
|
---|
[1f25a71] | 18 | font = nullptr;
|
---|
| 19 | fontSDLTexture = nullptr;
|
---|
| 20 | imageSDLTexture = nullptr;
|
---|
[87c8f1a] | 21 |
|
---|
| 22 | currentFrame = 0;
|
---|
| 23 | framebufferResized = false;
|
---|
[15104a8] | 24 |
|
---|
[055750a] | 25 | object_VP_mats = {};
|
---|
| 26 | ship_VP_mats = {};
|
---|
[0df3c9a] | 27 | }
|
---|
| 28 |
|
---|
[99d44b2] | 29 | VulkanGame::~VulkanGame() {
|
---|
[0df3c9a] | 30 | }
|
---|
| 31 |
|
---|
[b6e60b4] | 32 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
[2e77b3f] | 33 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
| 34 |
|
---|
| 35 | cout << "Vulkan Game" << endl;
|
---|
| 36 |
|
---|
[c559904] | 37 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
---|
| 38 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
---|
| 39 | SDL_GetVersion(&sdlVersion);
|
---|
| 40 |
|
---|
| 41 | // TODO: Refactor the logger api to be more flexible,
|
---|
| 42 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
| 43 | restart_gl_log();
|
---|
| 44 | gl_log("starting SDL\n%s.%s.%s",
|
---|
| 45 | to_string(sdlVersion.major).c_str(),
|
---|
| 46 | to_string(sdlVersion.minor).c_str(),
|
---|
| 47 | to_string(sdlVersion.patch).c_str());
|
---|
| 48 |
|
---|
| 49 | open_log();
|
---|
| 50 | get_log() << "starting SDL" << endl;
|
---|
| 51 | get_log() <<
|
---|
| 52 | (int)sdlVersion.major << "." <<
|
---|
| 53 | (int)sdlVersion.minor << "." <<
|
---|
| 54 | (int)sdlVersion.patch << endl;
|
---|
| 55 |
|
---|
[5edbd58] | 56 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
[0df3c9a] | 57 | return;
|
---|
| 58 | }
|
---|
[b6e60b4] | 59 |
|
---|
[0df3c9a] | 60 | initVulkan();
|
---|
| 61 | mainLoop();
|
---|
| 62 | cleanup();
|
---|
[c559904] | 63 |
|
---|
| 64 | close_log();
|
---|
[0df3c9a] | 65 | }
|
---|
| 66 |
|
---|
[0ae182f] | 67 | // TODO: Make some more init functions, or call this initUI if the
|
---|
[c559904] | 68 | // amount of things initialized here keeps growing
|
---|
[b6e60b4] | 69 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
[c559904] | 70 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
[0df3c9a] | 71 | gui = new GameGui_SDL();
|
---|
| 72 |
|
---|
[b6e60b4] | 73 | if (gui->init() == RTWO_ERROR) {
|
---|
[c559904] | 74 | // TODO: Also print these sorts of errors to the log
|
---|
[0df3c9a] | 75 | cout << "UI library could not be initialized!" << endl;
|
---|
[b6e60b4] | 76 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 77 | return RTWO_ERROR;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[b6e60b4] | 80 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
[0df3c9a] | 81 | if (window == nullptr) {
|
---|
| 82 | cout << "Window could not be created!" << endl;
|
---|
[ed7c953] | 83 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 84 | return RTWO_ERROR;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[b6e60b4] | 87 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
[a6f6833] | 88 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
[b6e60b4] | 89 |
|
---|
[c1d9b2a] | 90 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
---|
| 91 | if (renderer == nullptr) {
|
---|
| 92 | cout << "Renderer could not be created!" << endl;
|
---|
| 93 | cout << gui->getError() << endl;
|
---|
| 94 | return RTWO_ERROR;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[b794178] | 97 | SDL_VERSION(&sdlVersion);
|
---|
| 98 |
|
---|
[1f25a71] | 99 | cout << "SDL " << sdlVersion.major << "." << sdlVersion.minor << "." << sdlVersion.patch << endl;
|
---|
| 100 |
|
---|
| 101 | font = TTF_OpenFont("assets/fonts/lazy.ttf", 28);
|
---|
| 102 | if (font == nullptr) {
|
---|
| 103 | cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
| 104 | return RTWO_ERROR;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | SDL_Surface* fontSDLSurface = TTF_RenderText_Solid(font, "Great success!", { 255, 255, 255 });
|
---|
| 108 | if (fontSDLSurface == nullptr) {
|
---|
| 109 | cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
| 110 | return RTWO_ERROR;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | fontSDLTexture = SDL_CreateTextureFromSurface(renderer, fontSDLSurface);
|
---|
| 114 | if (fontSDLTexture == nullptr) {
|
---|
| 115 | cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
|
---|
| 116 | SDL_FreeSurface(fontSDLSurface);
|
---|
| 117 | return RTWO_ERROR;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | SDL_FreeSurface(fontSDLSurface);
|
---|
| 121 |
|
---|
| 122 | // TODO: Load a PNG instead
|
---|
| 123 | SDL_Surface* imageSDLSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
|
---|
| 124 | if (imageSDLSurface == nullptr) {
|
---|
| 125 | cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
|
---|
| 126 | return RTWO_ERROR;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | imageSDLTexture = SDL_CreateTextureFromSurface(renderer, imageSDLSurface);
|
---|
| 130 | if (imageSDLTexture == nullptr) {
|
---|
| 131 | cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
|
---|
| 132 | SDL_FreeSurface(imageSDLSurface);
|
---|
| 133 | return RTWO_ERROR;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | SDL_FreeSurface(imageSDLSurface);
|
---|
| 137 |
|
---|
[b794178] | 138 | // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
|
---|
| 139 | // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
|
---|
| 140 | // 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
|
---|
| 141 | // until the homebrew recipe is updated
|
---|
| 142 | if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
|
---|
| 143 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
| 144 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 145 | } else {
|
---|
| 146 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
---|
| 147 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | if (uiOverlay == nullptr) {
|
---|
| 151 | cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
|
---|
[1f25a71] | 152 | return RTWO_ERROR;
|
---|
[b794178] | 153 | }
|
---|
| 154 | if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
|
---|
| 155 | cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
|
---|
[1f25a71] | 156 | return RTWO_ERROR;
|
---|
[b794178] | 157 | }
|
---|
| 158 |
|
---|
[cd487fb] | 159 | SDL_SetRenderTarget(renderer, uiOverlay);
|
---|
| 160 |
|
---|
[0df3c9a] | 161 | return RTWO_SUCCESS;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[99d44b2] | 164 | void VulkanGame::initVulkan() {
|
---|
[c1d9b2a] | 165 | const vector<const char*> validationLayers = {
|
---|
| 166 | "VK_LAYER_KHRONOS_validation"
|
---|
| 167 | };
|
---|
[fe5c3ba] | 168 | const vector<const char*> deviceExtensions = {
|
---|
| 169 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
| 170 | };
|
---|
[c1d9b2a] | 171 |
|
---|
| 172 | createVulkanInstance(validationLayers);
|
---|
| 173 | setupDebugMessenger();
|
---|
[90a424f] | 174 | createVulkanSurface();
|
---|
[fe5c3ba] | 175 | pickPhysicalDevice(deviceExtensions);
|
---|
[c1c2021] | 176 | createLogicalDevice(validationLayers, deviceExtensions);
|
---|
[502bd0b] | 177 | createSwapChain();
|
---|
[f94eea9] | 178 | createImageViews();
|
---|
[6fc24c7] | 179 | createRenderPass();
|
---|
[fa9fa1c] | 180 | createCommandPool();
|
---|
[7d2b0b9] | 181 |
|
---|
[603b5bc] | 182 | createImageResources();
|
---|
| 183 | createFramebuffers();
|
---|
| 184 |
|
---|
[60578ce] | 185 | initMatrices();
|
---|
| 186 |
|
---|
[f97c5e7] | 187 | // TODO: Figure out how much of ubo creation and associated variables should be in the pipeline class
|
---|
| 188 | // Maybe combine the ubo-related objects into a new class
|
---|
| 189 |
|
---|
| 190 | initGraphicsPipelines();
|
---|
| 191 |
|
---|
| 192 | modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::pos));
|
---|
| 193 | modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::color));
|
---|
| 194 | modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord));
|
---|
| 195 |
|
---|
[055750a] | 196 | createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
| 197 | uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
|
---|
| 198 | createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
---|
| 199 | storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
|
---|
[f97c5e7] | 200 |
|
---|
| 201 | modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
---|
[055750a] | 202 | VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_scenePipeline);
|
---|
| 203 | modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
---|
| 204 | VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_scenePipeline);
|
---|
| 205 |
|
---|
[f97c5e7] | 206 | modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
| 207 | VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
|
---|
[5a0242e] | 208 |
|
---|
| 209 | modelPipeline.addObject({
|
---|
| 210 | {{-0.5f, -0.5f, -2.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 211 | {{ 0.5f, -0.5f, -2.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 212 | {{ 0.5f, 0.5f, -2.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 213 | {{-0.5f, 0.5f, -2.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
| 214 | }, {
|
---|
| 215 | 0, 1, 2, 2, 3, 0
|
---|
| 216 | }, commandPool, graphicsQueue);
|
---|
| 217 |
|
---|
| 218 | modelPipeline.addObject({
|
---|
| 219 | {{-0.5f, -0.5f, -1.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 220 | {{ 0.5f, -0.5f, -1.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 221 | {{ 0.5f, 0.5f, -1.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 222 | {{-0.5f, 0.5f, -1.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
| 223 | }, {
|
---|
| 224 | 0, 1, 2, 2, 3, 0
|
---|
| 225 | }, commandPool, graphicsQueue);
|
---|
[87c8f1a] | 226 |
|
---|
[b8777b7] | 227 | modelPipeline.createDescriptorSetLayout();
|
---|
| 228 | modelPipeline.createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
|
---|
| 229 | modelPipeline.createDescriptorPool(swapChainImages);
|
---|
| 230 | modelPipeline.createDescriptorSets(swapChainImages);
|
---|
[7d2b0b9] | 231 |
|
---|
[f97c5e7] | 232 | overlayPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
|
---|
| 233 | overlayPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
|
---|
| 234 |
|
---|
| 235 | overlayPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
| 236 | VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
|
---|
[771b33a] | 237 |
|
---|
[5a0242e] | 238 | overlayPipeline.addObject({
|
---|
| 239 | {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 240 | {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 241 | {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
|
---|
| 242 | {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}
|
---|
| 243 | }, {
|
---|
| 244 | 0, 1, 2, 2, 3, 0
|
---|
| 245 | }, commandPool, graphicsQueue);
|
---|
[87c8f1a] | 246 |
|
---|
[b8777b7] | 247 | overlayPipeline.createDescriptorSetLayout();
|
---|
| 248 | overlayPipeline.createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
|
---|
| 249 | overlayPipeline.createDescriptorPool(swapChainImages);
|
---|
| 250 | overlayPipeline.createDescriptorSets(swapChainImages);
|
---|
[7d2b0b9] | 251 |
|
---|
[3782d66] | 252 | shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::pos));
|
---|
| 253 | shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::color));
|
---|
[e1308e8] | 254 | shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::normal));
|
---|
[cf727ca] | 255 | shipPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ShipVertex::objIndex));
|
---|
[3782d66] | 256 |
|
---|
[055750a] | 257 | createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
| 258 | uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
|
---|
| 259 | createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
---|
| 260 | storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
|
---|
[3782d66] | 261 |
|
---|
| 262 | shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
---|
| 263 | VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline);
|
---|
[055750a] | 264 | shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
---|
| 265 | VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_shipPipeline);
|
---|
[3782d66] | 266 |
|
---|
[e1308e8] | 267 | // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly
|
---|
| 268 | // the same data. Add an option to make some pipelines not use indexing
|
---|
[785333b] | 269 | /*
|
---|
[cf727ca] | 270 | shipPipeline.addObject(
|
---|
| 271 | addObjectIndex<ShipVertex>(shipPipeline.getObjects().size(),
|
---|
| 272 | addVertexNormals<ShipVertex>({
|
---|
[3782d66] | 273 | //back
|
---|
| 274 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 275 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 276 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 277 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 278 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 279 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 280 |
|
---|
| 281 | // left back
|
---|
| 282 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 283 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 284 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 285 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 286 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 287 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 288 |
|
---|
| 289 | // right back
|
---|
| 290 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 291 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 292 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 293 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 294 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 295 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 296 |
|
---|
| 297 | // left mid
|
---|
| 298 | {{-0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 299 | {{-0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 300 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 301 | {{-0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 302 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 303 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 304 |
|
---|
| 305 | // right mid
|
---|
| 306 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 307 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 308 | {{ 0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 309 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 310 | {{ 0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 311 | {{ 0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 312 |
|
---|
| 313 | // left front
|
---|
| 314 | {{ 0.0f, 0.0f, -3.5f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 315 | {{-0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 316 | {{-0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 317 |
|
---|
| 318 | // right front
|
---|
| 319 | {{ 0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 320 | {{ 0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 321 | {{ 0.0f, 0.0f, -3.5f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 322 |
|
---|
| 323 | // top back
|
---|
| 324 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 325 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 326 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 327 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 328 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 329 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 330 |
|
---|
| 331 | // bottom back
|
---|
| 332 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 333 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 334 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 335 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 336 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 337 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 338 |
|
---|
| 339 | // top mid
|
---|
| 340 | {{-0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 341 | {{ -0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 342 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 343 | {{ -0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 344 | {{ 0.5f, 0.3f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 345 | {{ 0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 346 |
|
---|
| 347 | // bottom mid
|
---|
| 348 | {{ -0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 349 | {{-0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 350 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 351 | {{ 0.5f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 352 | {{-0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 353 | {{ 0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 1.0f}},
|
---|
| 354 |
|
---|
| 355 | // top front
|
---|
| 356 | {{-0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 357 | {{ 0.25f, 0.3f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 358 | {{ 0.0f, 0.0f, -3.5f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 359 |
|
---|
| 360 | // bottom front
|
---|
| 361 | {{ 0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 362 | {{-0.25f, 0.0f, -3.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 363 | {{ 0.0f, 0.0f, -3.5f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 364 |
|
---|
| 365 | // left wing start back
|
---|
| 366 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 367 | {{ -1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 368 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 369 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 370 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 371 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 372 |
|
---|
| 373 | // left wing start top
|
---|
| 374 | {{ -0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 375 | {{ -1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 376 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 377 | {{ -0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 378 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 379 | {{ -0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 380 |
|
---|
| 381 | // left wing start front
|
---|
| 382 | {{ -0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 383 | {{ -0.5f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 384 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 385 | {{ -0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 386 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 387 | {{ -1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 388 |
|
---|
| 389 | // left wing start bottom
|
---|
| 390 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 391 | {{ -1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 392 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 393 | {{ -0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 394 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 395 | {{ -0.5f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 396 |
|
---|
| 397 | // left wing end outside
|
---|
| 398 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 399 | {{ -2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 400 | {{ -1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 401 |
|
---|
| 402 | // left wing end top
|
---|
| 403 | {{ -1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 404 | {{ -2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 405 | {{ -1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 406 |
|
---|
| 407 | // left wing end front
|
---|
| 408 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 409 | {{ -2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 410 | {{ -1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 411 |
|
---|
| 412 | // left wing end bottom
|
---|
| 413 | {{ -1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 414 | {{ -2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 415 | {{ -1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 416 |
|
---|
| 417 | // right wing start back
|
---|
| 418 | {{ 1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 419 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 420 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 421 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 422 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 423 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 424 |
|
---|
| 425 | // right wing start top
|
---|
| 426 | {{ 1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 427 | {{ 0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 428 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 429 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 430 | {{ 0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 431 | {{ 0.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 432 |
|
---|
| 433 | // right wing start front
|
---|
| 434 | {{ 0.5f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 435 | {{ 0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 436 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 437 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 438 | {{ 0.5f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 439 | {{ 1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 440 |
|
---|
| 441 | // right wing start bottom
|
---|
| 442 | {{ 1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 443 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 444 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 445 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 446 | {{ 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 447 | {{ 0.5f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 448 |
|
---|
| 449 | // right wing end outside
|
---|
| 450 | {{ 2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 451 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 452 | {{ 1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 453 |
|
---|
| 454 | // right wing end top
|
---|
| 455 | {{ 2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 456 | {{ 1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 457 | {{ 1.5f, 0.3f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 458 |
|
---|
| 459 | // right wing end front
|
---|
| 460 | {{ 2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 461 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 462 | {{ 1.3f, 0.3f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 463 |
|
---|
| 464 | // right wing end bottom
|
---|
| 465 | {{ 2.2f, 0.15f, -0.8f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 466 | {{ 1.5f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.3f}},
|
---|
| 467 | {{ 1.3f, 0.0f, -0.3f}, {0.0f, 0.0f, 0.3f}},
|
---|
[cf727ca] | 468 | })), {
|
---|
[3782d66] | 469 | 0, 1, 2, 3, 4, 5,
|
---|
| 470 | 6, 7, 8, 9, 10, 11,
|
---|
| 471 | 12, 13, 14, 15, 16, 17,
|
---|
| 472 | 18, 19, 20, 21, 22, 23,
|
---|
| 473 | 24, 25, 26, 27, 28, 29,
|
---|
| 474 | 30, 31, 32,
|
---|
| 475 | 33, 34, 35,
|
---|
| 476 | 36, 37, 38, 39, 40, 41,
|
---|
| 477 | 42, 43, 44, 45, 46, 47,
|
---|
| 478 | 48, 49, 50, 51, 52, 53,
|
---|
| 479 | 54, 55, 56, 57, 58, 59,
|
---|
| 480 | 60, 61, 62,
|
---|
| 481 | 63, 64, 65,
|
---|
| 482 | 66, 67, 68, 69, 70, 71,
|
---|
| 483 | 72, 73, 74, 75, 76, 77,
|
---|
| 484 | 78, 79, 80, 81, 82, 83,
|
---|
| 485 | 84, 85, 86, 87, 88, 89,
|
---|
| 486 | 90, 91, 92,
|
---|
| 487 | 93, 94, 95,
|
---|
| 488 | 96, 97, 98,
|
---|
| 489 | 99, 100, 101,
|
---|
| 490 | 102, 103, 104, 105, 106, 107,
|
---|
| 491 | 108, 109, 110, 111, 112, 113,
|
---|
| 492 | 114, 115, 116, 117, 118, 119,
|
---|
| 493 | 120, 121, 122, 123, 124, 125,
|
---|
| 494 | 126, 127, 128,
|
---|
| 495 | 129, 130, 131,
|
---|
| 496 | 132, 133, 134,
|
---|
| 497 | 135, 136, 137,
|
---|
| 498 | }, commandPool, graphicsQueue);
|
---|
[785333b] | 499 | */
|
---|
| 500 |
|
---|
| 501 | // z-range is 0 to 1, with +1 pointing into the screen
|
---|
[7c929fc] | 502 | shipPipeline.addObject(
|
---|
| 503 | addObjectIndex<ShipVertex>(shipPipeline.getObjects().size(),
|
---|
| 504 | addVertexNormals<ShipVertex>({
|
---|
[60578ce] | 505 | {{ 40.0f, 40.0f, 72.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 506 | {{ -40.0f, 40.0f, 80.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 507 | {{ -40.0f, -40.0f, 80.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 508 | {{ 40.0f, 40.0f, 72.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 509 | {{ -40.0f, -40.0f, 80.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 510 | {{ 40.0f, -40.0f, 72.0f}, {0.0f, 0.6f, 0.0f}},
|
---|
| 511 |
|
---|
| 512 | {{ 8.0f, 8.0f, 34.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
| 513 | {{ -8.0f, 8.0f, 30.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
| 514 | {{ -8.0f, -8.0f, 30.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
| 515 | {{ 8.0f, 8.0f, 34.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
| 516 | {{ -8.0f, -8.0f, 30.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
| 517 | {{ 8.0f, -8.0f, 34.0f}, {0.0f, 0.0f, 0.7f}},
|
---|
[7c929fc] | 518 | })), {
|
---|
[785333b] | 519 | 0, 1, 2, 3, 4, 5,
|
---|
| 520 | 6, 7, 8, 9, 10, 11,
|
---|
[7c929fc] | 521 | }, commandPool, graphicsQueue);
|
---|
[3782d66] | 522 |
|
---|
| 523 | shipPipeline.createDescriptorSetLayout();
|
---|
| 524 | shipPipeline.createPipeline("shaders/ship-vert.spv", "shaders/ship-frag.spv");
|
---|
| 525 | shipPipeline.createDescriptorPool(swapChainImages);
|
---|
| 526 | shipPipeline.createDescriptorSets(swapChainImages);
|
---|
| 527 |
|
---|
[b8777b7] | 528 | cout << "Created all the graphics pipelines" << endl;
|
---|
[34bdf3a] | 529 |
|
---|
| 530 | createCommandBuffers();
|
---|
| 531 |
|
---|
| 532 | createSyncObjects();
|
---|
[0df3c9a] | 533 | }
|
---|
| 534 |
|
---|
[f97c5e7] | 535 | void VulkanGame::initGraphicsPipelines() {
|
---|
| 536 | modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass,
|
---|
| 537 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24);
|
---|
| 538 |
|
---|
| 539 | overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass,
|
---|
| 540 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
|
---|
[3782d66] | 541 |
|
---|
| 542 | shipPipeline = GraphicsPipeline_Vulkan<ShipVertex>(physicalDevice, device, renderPass,
|
---|
| 543 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 138, 138);
|
---|
[f97c5e7] | 544 | }
|
---|
| 545 |
|
---|
[683dd55] | 546 | // TODO: Maybe changes the name to initScene() or something similar
|
---|
[15104a8] | 547 | void VulkanGame::initMatrices() {
|
---|
| 548 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
| 549 |
|
---|
| 550 | float cam_yaw = 0.0f;
|
---|
[60578ce] | 551 | float cam_pitch = 0.0f; // -50.0f;
|
---|
[15104a8] | 552 |
|
---|
| 553 | mat4 yaw_mat = rotate(mat4(1.0f), radians(-cam_yaw), vec3(0.0f, 1.0f, 0.0f));
|
---|
| 554 | mat4 pitch_mat = rotate(mat4(1.0f), radians(-cam_pitch), vec3(1.0f, 0.0f, 0.0f));
|
---|
| 555 |
|
---|
[f97c5e7] | 556 | mat4 R_view = pitch_mat * yaw_mat;
|
---|
| 557 | mat4 T_view = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
| 558 | mat4 view = R_view * T_view;
|
---|
[15104a8] | 559 |
|
---|
[f97c5e7] | 560 | mat4 proj = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
|
---|
| 561 | proj[1][1] *= -1; // flip the y-axis so that +y is up
|
---|
[15104a8] | 562 |
|
---|
[055750a] | 563 | object_VP_mats.view = view;
|
---|
| 564 | object_VP_mats.proj = proj;
|
---|
[3782d66] | 565 |
|
---|
[055750a] | 566 | ship_VP_mats.view = view;
|
---|
| 567 | ship_VP_mats.proj = proj;
|
---|
[15104a8] | 568 | }
|
---|
| 569 |
|
---|
[99d44b2] | 570 | void VulkanGame::mainLoop() {
|
---|
[f6521fb] | 571 | UIEvent e;
|
---|
[0df3c9a] | 572 | bool quit = false;
|
---|
| 573 |
|
---|
| 574 | while (!quit) {
|
---|
[27c40ce] | 575 | gui->processEvents();
|
---|
| 576 |
|
---|
[f6521fb] | 577 | while (gui->pollEvent(&e)) {
|
---|
| 578 | switch(e.type) {
|
---|
| 579 | case UI_EVENT_QUIT:
|
---|
| 580 | cout << "Quit event detected" << endl;
|
---|
| 581 | quit = true;
|
---|
| 582 | break;
|
---|
| 583 | case UI_EVENT_WINDOW:
|
---|
| 584 | cout << "Window event detected" << endl;
|
---|
| 585 | // Currently unused
|
---|
| 586 | break;
|
---|
[0e09340] | 587 | case UI_EVENT_WINDOWRESIZE:
|
---|
| 588 | cout << "Window resize event detected" << endl;
|
---|
| 589 | framebufferResized = true;
|
---|
| 590 | break;
|
---|
[e3bef3a] | 591 | case UI_EVENT_KEYDOWN:
|
---|
[f6521fb] | 592 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
| 593 | quit = true;
|
---|
[e3bef3a] | 594 | } else if (e.key.keycode == SDL_SCANCODE_SPACE) {
|
---|
| 595 | cout << "Adding a plane" << endl;
|
---|
[683dd55] | 596 | float zOffset = -2.0f + (0.5f * modelPipeline.getObjects().size());
|
---|
[5a0242e] | 597 |
|
---|
| 598 | vkDeviceWaitIdle(device);
|
---|
| 599 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 600 |
|
---|
| 601 | modelPipeline.addObject({
|
---|
[e3bef3a] | 602 | {{-0.5f, -0.5f, zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
| 603 | {{ 0.5f, -0.5f, zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
| 604 | {{ 0.5f, 0.5f, zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
| 605 | {{-0.5f, 0.5f, zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
[5a0242e] | 606 | }, {
|
---|
[e3bef3a] | 607 | 0, 1, 2, 2, 3, 0
|
---|
[5a0242e] | 608 | }, commandPool, graphicsQueue);
|
---|
[b8777b7] | 609 |
|
---|
| 610 | createCommandBuffers();
|
---|
[f6521fb] | 611 | } else {
|
---|
| 612 | cout << "Key event detected" << endl;
|
---|
| 613 | }
|
---|
| 614 | break;
|
---|
[5a0242e] | 615 | case UI_EVENT_KEYUP:
|
---|
| 616 | break;
|
---|
[f6521fb] | 617 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
| 618 | cout << "Mouse button down event detected" << endl;
|
---|
| 619 | break;
|
---|
| 620 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
| 621 | cout << "Mouse button up event detected" << endl;
|
---|
| 622 | break;
|
---|
| 623 | case UI_EVENT_MOUSEMOTION:
|
---|
| 624 | break;
|
---|
[a0da009] | 625 | case UI_EVENT_UNKNOWN:
|
---|
| 626 | cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;
|
---|
| 627 | break;
|
---|
[c61323a] | 628 | default:
|
---|
| 629 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
[0df3c9a] | 630 | }
|
---|
| 631 | }
|
---|
[c1d9b2a] | 632 |
|
---|
[a0c5f28] | 633 | renderUI();
|
---|
| 634 | renderScene();
|
---|
[0df3c9a] | 635 | }
|
---|
[c1c2021] | 636 |
|
---|
| 637 | vkDeviceWaitIdle(device);
|
---|
[0df3c9a] | 638 | }
|
---|
| 639 |
|
---|
[055750a] | 640 | // TODO: The view and projection mats only need to be updated once.
|
---|
| 641 | // Create a separate function that can do this once per Vulkan image at the beginning
|
---|
[8e02b6b] | 642 | void VulkanGame::updateScene(uint32_t currentImage) {
|
---|
| 643 | static auto startTime = chrono::high_resolution_clock::now();
|
---|
| 644 |
|
---|
| 645 | auto currentTime = chrono::high_resolution_clock::now();
|
---|
| 646 | float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
|
---|
| 647 |
|
---|
[055750a] | 648 | so_Object.model =
|
---|
[8e02b6b] | 649 | translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
|
---|
| 650 | rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
|
---|
| 651 |
|
---|
[055750a] | 652 | so_Ship.model =
|
---|
| 653 | translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
|
---|
| 654 | scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
|
---|
| 655 |
|
---|
| 656 | VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats);
|
---|
| 657 |
|
---|
| 658 | VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage], so_Object);
|
---|
[3782d66] | 659 |
|
---|
[055750a] | 660 | VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], ship_VP_mats);
|
---|
| 661 |
|
---|
| 662 | VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship);
|
---|
[8e02b6b] | 663 | }
|
---|
| 664 |
|
---|
[a0c5f28] | 665 | void VulkanGame::renderUI() {
|
---|
[d2d9286] | 666 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
---|
[a0c5f28] | 667 | SDL_RenderClear(renderer);
|
---|
[d2d9286] | 668 |
|
---|
| 669 | SDL_Rect rect = {280, 220, 100, 100};
|
---|
| 670 | SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
---|
| 671 | SDL_RenderFillRect(renderer, &rect);
|
---|
| 672 |
|
---|
[1f25a71] | 673 | rect = {10, 10, 0, 0};
|
---|
| 674 | SDL_QueryTexture(fontSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
| 675 | SDL_RenderCopy(renderer, fontSDLTexture, nullptr, &rect);
|
---|
| 676 |
|
---|
| 677 | rect = {10, 80, 0, 0};
|
---|
| 678 | SDL_QueryTexture(imageSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
| 679 | SDL_RenderCopy(renderer, imageSDLTexture, nullptr, &rect);
|
---|
| 680 |
|
---|
[d2d9286] | 681 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
|
---|
| 682 | SDL_RenderDrawLine(renderer, 50, 5, 150, 500);
|
---|
| 683 |
|
---|
| 684 | VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer,
|
---|
| 685 | sdlOverlayImage, graphicsQueue);
|
---|
[a0c5f28] | 686 | }
|
---|
| 687 |
|
---|
| 688 | void VulkanGame::renderScene() {
|
---|
[87c8f1a] | 689 | vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
|
---|
| 690 |
|
---|
| 691 | uint32_t imageIndex;
|
---|
| 692 |
|
---|
[d2d9286] | 693 | VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
|
---|
| 694 | imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
---|
| 695 |
|
---|
| 696 | if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
---|
| 697 | recreateSwapChain();
|
---|
| 698 | return;
|
---|
| 699 | } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
|
---|
| 700 | throw runtime_error("failed to acquire swap chain image!");
|
---|
| 701 | }
|
---|
| 702 |
|
---|
[8e02b6b] | 703 | // TODO: Figure out a more elegant way to only do updates and render the UI once per scene render
|
---|
| 704 | // Probably move some of the renderScene() code into a higher function that updates the UI, and renders
|
---|
| 705 | // the UI and scene
|
---|
| 706 | updateScene(imageIndex);
|
---|
[f985231] | 707 |
|
---|
[d2d9286] | 708 | VkSubmitInfo submitInfo = {};
|
---|
| 709 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
---|
| 710 |
|
---|
| 711 | VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
|
---|
| 712 | VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
---|
| 713 |
|
---|
| 714 | submitInfo.waitSemaphoreCount = 1;
|
---|
| 715 | submitInfo.pWaitSemaphores = waitSemaphores;
|
---|
| 716 | submitInfo.pWaitDstStageMask = waitStages;
|
---|
| 717 | submitInfo.commandBufferCount = 1;
|
---|
| 718 | submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
|
---|
| 719 |
|
---|
| 720 | VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
|
---|
| 721 |
|
---|
| 722 | submitInfo.signalSemaphoreCount = 1;
|
---|
| 723 | submitInfo.pSignalSemaphores = signalSemaphores;
|
---|
| 724 |
|
---|
| 725 | vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
---|
| 726 |
|
---|
| 727 | if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
|
---|
| 728 | throw runtime_error("failed to submit draw command buffer!");
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | VkPresentInfoKHR presentInfo = {};
|
---|
| 732 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
---|
| 733 | presentInfo.waitSemaphoreCount = 1;
|
---|
| 734 | presentInfo.pWaitSemaphores = signalSemaphores;
|
---|
| 735 |
|
---|
| 736 | VkSwapchainKHR swapChains[] = { swapChain };
|
---|
| 737 | presentInfo.swapchainCount = 1;
|
---|
| 738 | presentInfo.pSwapchains = swapChains;
|
---|
| 739 | presentInfo.pImageIndices = &imageIndex;
|
---|
| 740 | presentInfo.pResults = nullptr;
|
---|
| 741 |
|
---|
| 742 | result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
---|
| 743 |
|
---|
| 744 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
|
---|
| 745 | framebufferResized = false;
|
---|
| 746 | recreateSwapChain();
|
---|
| 747 | } else if (result != VK_SUCCESS) {
|
---|
| 748 | throw runtime_error("failed to present swap chain image!");
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[87c8f1a] | 751 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
| 752 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
[a0c5f28] | 753 | }
|
---|
| 754 |
|
---|
[99d44b2] | 755 | void VulkanGame::cleanup() {
|
---|
[c1c2021] | 756 | cleanupSwapChain();
|
---|
| 757 |
|
---|
[e83b155] | 758 | VulkanUtils::destroyVulkanImage(device, floorTextureImage);
|
---|
| 759 | VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);
|
---|
| 760 |
|
---|
| 761 | vkDestroySampler(device, textureSampler, nullptr);
|
---|
| 762 |
|
---|
[b8777b7] | 763 | modelPipeline.cleanupBuffers();
|
---|
| 764 | overlayPipeline.cleanupBuffers();
|
---|
[3782d66] | 765 | shipPipeline.cleanupBuffers();
|
---|
[b794178] | 766 |
|
---|
[34bdf3a] | 767 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
| 768 | vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
---|
| 769 | vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
---|
| 770 | vkDestroyFence(device, inFlightFences[i], nullptr);
|
---|
| 771 | }
|
---|
| 772 |
|
---|
[fa9fa1c] | 773 | vkDestroyCommandPool(device, commandPool, nullptr);
|
---|
[c1c2021] | 774 | vkDestroyDevice(device, nullptr);
|
---|
| 775 | vkDestroySurfaceKHR(instance, surface, nullptr);
|
---|
| 776 |
|
---|
[c1d9b2a] | 777 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 778 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
| 779 | }
|
---|
[c1c2021] | 780 |
|
---|
[c1d9b2a] | 781 | vkDestroyInstance(instance, nullptr);
|
---|
| 782 |
|
---|
[b794178] | 783 | // TODO: Check if any of these functions accept null parameters
|
---|
| 784 | // If they do, I don't need to check for that
|
---|
| 785 |
|
---|
| 786 | if (uiOverlay != nullptr) {
|
---|
| 787 | SDL_DestroyTexture(uiOverlay);
|
---|
| 788 | uiOverlay = nullptr;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
[1f25a71] | 791 | if (fontSDLTexture != nullptr) {
|
---|
| 792 | SDL_DestroyTexture(fontSDLTexture);
|
---|
| 793 | fontSDLTexture = nullptr;
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | if (imageSDLTexture != nullptr) {
|
---|
| 797 | SDL_DestroyTexture(imageSDLTexture);
|
---|
| 798 | imageSDLTexture = nullptr;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | TTF_CloseFont(font);
|
---|
| 802 | font = nullptr;
|
---|
| 803 |
|
---|
[c1d9b2a] | 804 | SDL_DestroyRenderer(renderer);
|
---|
| 805 | renderer = nullptr;
|
---|
| 806 |
|
---|
[b6e60b4] | 807 | gui->destroyWindow();
|
---|
| 808 | gui->shutdown();
|
---|
[0df3c9a] | 809 | delete gui;
|
---|
[c1d9b2a] | 810 | }
|
---|
| 811 |
|
---|
| 812 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
---|
| 813 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
---|
| 814 | throw runtime_error("validation layers requested, but not available!");
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | VkApplicationInfo appInfo = {};
|
---|
| 818 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
| 819 | appInfo.pApplicationName = "Vulkan Game";
|
---|
| 820 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
| 821 | appInfo.pEngineName = "No Engine";
|
---|
| 822 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
| 823 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
| 824 |
|
---|
| 825 | VkInstanceCreateInfo createInfo = {};
|
---|
| 826 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
| 827 | createInfo.pApplicationInfo = &appInfo;
|
---|
| 828 |
|
---|
| 829 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
| 830 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 831 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
| 835 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
| 836 |
|
---|
| 837 | cout << endl << "Extensions:" << endl;
|
---|
| 838 | for (const char* extensionName : extensions) {
|
---|
| 839 | cout << extensionName << endl;
|
---|
| 840 | }
|
---|
| 841 | cout << endl;
|
---|
| 842 |
|
---|
| 843 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
| 844 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 845 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
| 846 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
| 847 |
|
---|
| 848 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
| 849 | createInfo.pNext = &debugCreateInfo;
|
---|
| 850 | } else {
|
---|
| 851 | createInfo.enabledLayerCount = 0;
|
---|
| 852 |
|
---|
| 853 | createInfo.pNext = nullptr;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
| 857 | throw runtime_error("failed to create instance!");
|
---|
| 858 | }
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 | void VulkanGame::setupDebugMessenger() {
|
---|
| 862 | if (!ENABLE_VALIDATION_LAYERS) return;
|
---|
| 863 |
|
---|
| 864 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
| 865 | populateDebugMessengerCreateInfo(createInfo);
|
---|
| 866 |
|
---|
| 867 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
| 868 | throw runtime_error("failed to set up debug messenger!");
|
---|
| 869 | }
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
| 873 | createInfo = {};
|
---|
| 874 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
| 875 | 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;
|
---|
| 876 | 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;
|
---|
| 877 | createInfo.pfnUserCallback = debugCallback;
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
---|
| 881 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 882 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 883 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 884 | void* pUserData) {
|
---|
| 885 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
| 886 |
|
---|
| 887 | return VK_FALSE;
|
---|
| 888 | }
|
---|
[90a424f] | 889 |
|
---|
| 890 | void VulkanGame::createVulkanSurface() {
|
---|
| 891 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
| 892 | throw runtime_error("failed to create window surface!");
|
---|
| 893 | }
|
---|
| 894 | }
|
---|
| 895 |
|
---|
[fe5c3ba] | 896 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 897 | uint32_t deviceCount = 0;
|
---|
| 898 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
| 899 |
|
---|
| 900 | if (deviceCount == 0) {
|
---|
| 901 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
| 905 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
| 906 |
|
---|
| 907 | cout << endl << "Graphics cards:" << endl;
|
---|
| 908 | for (const VkPhysicalDevice& device : devices) {
|
---|
[fe5c3ba] | 909 | if (isDeviceSuitable(device, deviceExtensions)) {
|
---|
[90a424f] | 910 | physicalDevice = device;
|
---|
| 911 | break;
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 | cout << endl;
|
---|
| 915 |
|
---|
| 916 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
| 917 | throw runtime_error("failed to find a suitable GPU!");
|
---|
| 918 | }
|
---|
| 919 | }
|
---|
| 920 |
|
---|
[fa9fa1c] | 921 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice physicalDevice,
|
---|
| 922 | const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 923 | VkPhysicalDeviceProperties deviceProperties;
|
---|
[fa9fa1c] | 924 | vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
|
---|
[90a424f] | 925 |
|
---|
| 926 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
| 927 |
|
---|
[fa9fa1c] | 928 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 929 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(physicalDevice, deviceExtensions);
|
---|
[90a424f] | 930 | bool swapChainAdequate = false;
|
---|
| 931 |
|
---|
| 932 | if (extensionsSupported) {
|
---|
[fa9fa1c] | 933 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
[90a424f] | 934 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
[fa9fa1c] | 938 | vkGetPhysicalDeviceFeatures(physicalDevice, &supportedFeatures);
|
---|
[90a424f] | 939 |
|
---|
| 940 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
[c1c2021] | 941 | }
|
---|
| 942 |
|
---|
| 943 | void VulkanGame::createLogicalDevice(
|
---|
[055750a] | 944 | const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) {
|
---|
[c1c2021] | 945 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 946 |
|
---|
[b794178] | 947 | vector<VkDeviceQueueCreateInfo> queueCreateInfoList;
|
---|
[c1c2021] | 948 | set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
| 949 |
|
---|
| 950 | float queuePriority = 1.0f;
|
---|
| 951 | for (uint32_t queueFamily : uniqueQueueFamilies) {
|
---|
| 952 | VkDeviceQueueCreateInfo queueCreateInfo = {};
|
---|
| 953 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
---|
| 954 | queueCreateInfo.queueFamilyIndex = queueFamily;
|
---|
| 955 | queueCreateInfo.queueCount = 1;
|
---|
| 956 | queueCreateInfo.pQueuePriorities = &queuePriority;
|
---|
| 957 |
|
---|
[b794178] | 958 | queueCreateInfoList.push_back(queueCreateInfo);
|
---|
[c1c2021] | 959 | }
|
---|
| 960 |
|
---|
| 961 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
---|
| 962 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
---|
| 963 |
|
---|
| 964 | VkDeviceCreateInfo createInfo = {};
|
---|
| 965 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
---|
[b794178] | 966 | createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfoList.size());
|
---|
| 967 | createInfo.pQueueCreateInfos = queueCreateInfoList.data();
|
---|
[c1c2021] | 968 |
|
---|
| 969 | createInfo.pEnabledFeatures = &deviceFeatures;
|
---|
| 970 |
|
---|
| 971 | createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
---|
| 972 | createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
---|
| 973 |
|
---|
| 974 | // These fields are ignored by up-to-date Vulkan implementations,
|
---|
| 975 | // but it's a good idea to set them for backwards compatibility
|
---|
| 976 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
| 977 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
| 978 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
| 979 | } else {
|
---|
| 980 | createInfo.enabledLayerCount = 0;
|
---|
| 981 | }
|
---|
| 982 |
|
---|
| 983 | if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
---|
| 984 | throw runtime_error("failed to create logical device!");
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
---|
| 988 | vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
---|
[502bd0b] | 989 | }
|
---|
| 990 |
|
---|
| 991 | void VulkanGame::createSwapChain() {
|
---|
| 992 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
| 993 |
|
---|
| 994 | VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
|
---|
| 995 | VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
|
---|
| 996 | VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
|
---|
| 997 |
|
---|
| 998 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
---|
| 999 | if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
|
---|
| 1000 | imageCount = swapChainSupport.capabilities.maxImageCount;
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | VkSwapchainCreateInfoKHR createInfo = {};
|
---|
| 1004 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
---|
| 1005 | createInfo.surface = surface;
|
---|
| 1006 | createInfo.minImageCount = imageCount;
|
---|
| 1007 | createInfo.imageFormat = surfaceFormat.format;
|
---|
| 1008 | createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
---|
| 1009 | createInfo.imageExtent = extent;
|
---|
| 1010 | createInfo.imageArrayLayers = 1;
|
---|
| 1011 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
---|
| 1012 |
|
---|
| 1013 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
| 1014 | uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
| 1015 |
|
---|
| 1016 | if (indices.graphicsFamily != indices.presentFamily) {
|
---|
| 1017 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
---|
| 1018 | createInfo.queueFamilyIndexCount = 2;
|
---|
| 1019 | createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
---|
[f94eea9] | 1020 | } else {
|
---|
[502bd0b] | 1021 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
| 1022 | createInfo.queueFamilyIndexCount = 0;
|
---|
| 1023 | createInfo.pQueueFamilyIndices = nullptr;
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
---|
| 1027 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
---|
| 1028 | createInfo.presentMode = presentMode;
|
---|
| 1029 | createInfo.clipped = VK_TRUE;
|
---|
| 1030 | createInfo.oldSwapchain = VK_NULL_HANDLE;
|
---|
| 1031 |
|
---|
| 1032 | if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
|
---|
| 1033 | throw runtime_error("failed to create swap chain!");
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
---|
| 1037 | swapChainImages.resize(imageCount);
|
---|
| 1038 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
---|
| 1039 |
|
---|
| 1040 | swapChainImageFormat = surfaceFormat.format;
|
---|
[603b5bc] | 1041 | swapChainExtent = extent;
|
---|
[f94eea9] | 1042 | }
|
---|
| 1043 |
|
---|
| 1044 | void VulkanGame::createImageViews() {
|
---|
| 1045 | swapChainImageViews.resize(swapChainImages.size());
|
---|
| 1046 |
|
---|
| 1047 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
| 1048 | swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainImageFormat,
|
---|
| 1049 | VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
| 1050 | }
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
[6fc24c7] | 1053 | void VulkanGame::createRenderPass() {
|
---|
| 1054 | VkAttachmentDescription colorAttachment = {};
|
---|
| 1055 | colorAttachment.format = swapChainImageFormat;
|
---|
| 1056 | colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 1057 | colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
| 1058 | colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
---|
| 1059 | colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
| 1060 | colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 1061 | colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
| 1062 | colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
---|
| 1063 |
|
---|
| 1064 | VkAttachmentReference colorAttachmentRef = {};
|
---|
| 1065 | colorAttachmentRef.attachment = 0;
|
---|
| 1066 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
---|
| 1067 |
|
---|
| 1068 | VkAttachmentDescription depthAttachment = {};
|
---|
| 1069 | depthAttachment.format = findDepthFormat();
|
---|
| 1070 | depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 1071 | depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
| 1072 | depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 1073 | depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
| 1074 | depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
| 1075 | depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
| 1076 | depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
| 1077 |
|
---|
| 1078 | VkAttachmentReference depthAttachmentRef = {};
|
---|
| 1079 | depthAttachmentRef.attachment = 1;
|
---|
| 1080 | depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
| 1081 |
|
---|
| 1082 | VkSubpassDescription subpass = {};
|
---|
| 1083 | subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
---|
| 1084 | subpass.colorAttachmentCount = 1;
|
---|
| 1085 | subpass.pColorAttachments = &colorAttachmentRef;
|
---|
| 1086 | subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
---|
| 1087 |
|
---|
| 1088 | VkSubpassDependency dependency = {};
|
---|
| 1089 | dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
---|
| 1090 | dependency.dstSubpass = 0;
|
---|
| 1091 | dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
| 1092 | dependency.srcAccessMask = 0;
|
---|
| 1093 | dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
| 1094 | dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
---|
| 1095 |
|
---|
| 1096 | array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
|
---|
| 1097 | VkRenderPassCreateInfo renderPassInfo = {};
|
---|
| 1098 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
---|
| 1099 | renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
| 1100 | renderPassInfo.pAttachments = attachments.data();
|
---|
| 1101 | renderPassInfo.subpassCount = 1;
|
---|
| 1102 | renderPassInfo.pSubpasses = &subpass;
|
---|
| 1103 | renderPassInfo.dependencyCount = 1;
|
---|
| 1104 | renderPassInfo.pDependencies = &dependency;
|
---|
| 1105 |
|
---|
| 1106 | if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
|
---|
| 1107 | throw runtime_error("failed to create render pass!");
|
---|
| 1108 | }
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 | VkFormat VulkanGame::findDepthFormat() {
|
---|
| 1112 | return VulkanUtils::findSupportedFormat(
|
---|
| 1113 | physicalDevice,
|
---|
| 1114 | { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
|
---|
| 1115 | VK_IMAGE_TILING_OPTIMAL,
|
---|
| 1116 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
|
---|
| 1117 | );
|
---|
| 1118 | }
|
---|
| 1119 |
|
---|
[fa9fa1c] | 1120 | void VulkanGame::createCommandPool() {
|
---|
| 1121 | QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);;
|
---|
| 1122 |
|
---|
| 1123 | VkCommandPoolCreateInfo poolInfo = {};
|
---|
| 1124 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
---|
| 1125 | poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
|
---|
| 1126 | poolInfo.flags = 0;
|
---|
| 1127 |
|
---|
| 1128 | if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
|
---|
| 1129 | throw runtime_error("failed to create graphics command pool!");
|
---|
| 1130 | }
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
[603b5bc] | 1133 | void VulkanGame::createImageResources() {
|
---|
| 1134 | VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
|
---|
| 1135 | depthImage, graphicsQueue);
|
---|
[b794178] | 1136 |
|
---|
[603b5bc] | 1137 | createTextureSampler();
|
---|
[b794178] | 1138 |
|
---|
| 1139 | VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
|
---|
| 1140 | floorTextureImage, graphicsQueue);
|
---|
| 1141 |
|
---|
| 1142 | floorTextureImageDescriptor = {};
|
---|
| 1143 | floorTextureImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
| 1144 | floorTextureImageDescriptor.imageView = floorTextureImage.imageView;
|
---|
| 1145 | floorTextureImageDescriptor.sampler = textureSampler;
|
---|
| 1146 |
|
---|
[603b5bc] | 1147 | VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
|
---|
| 1148 |
|
---|
[b794178] | 1149 | sdlOverlayImageDescriptor = {};
|
---|
| 1150 | sdlOverlayImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
| 1151 | sdlOverlayImageDescriptor.imageView = sdlOverlayImage.imageView;
|
---|
| 1152 | sdlOverlayImageDescriptor.sampler = textureSampler;
|
---|
| 1153 | }
|
---|
| 1154 |
|
---|
| 1155 | void VulkanGame::createTextureSampler() {
|
---|
| 1156 | VkSamplerCreateInfo samplerInfo = {};
|
---|
| 1157 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
---|
| 1158 | samplerInfo.magFilter = VK_FILTER_LINEAR;
|
---|
| 1159 | samplerInfo.minFilter = VK_FILTER_LINEAR;
|
---|
| 1160 |
|
---|
| 1161 | samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 1162 | samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 1163 | samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
| 1164 |
|
---|
| 1165 | samplerInfo.anisotropyEnable = VK_TRUE;
|
---|
| 1166 | samplerInfo.maxAnisotropy = 16;
|
---|
| 1167 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
---|
| 1168 | samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
---|
| 1169 | samplerInfo.compareEnable = VK_FALSE;
|
---|
| 1170 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
---|
| 1171 | samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
---|
| 1172 | samplerInfo.mipLodBias = 0.0f;
|
---|
| 1173 | samplerInfo.minLod = 0.0f;
|
---|
| 1174 | samplerInfo.maxLod = 0.0f;
|
---|
| 1175 |
|
---|
| 1176 | if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
|
---|
| 1177 | throw runtime_error("failed to create texture sampler!");
|
---|
| 1178 | }
|
---|
| 1179 | }
|
---|
| 1180 |
|
---|
[603b5bc] | 1181 | void VulkanGame::createFramebuffers() {
|
---|
| 1182 | swapChainFramebuffers.resize(swapChainImageViews.size());
|
---|
| 1183 |
|
---|
| 1184 | for (size_t i = 0; i < swapChainImageViews.size(); i++) {
|
---|
| 1185 | array<VkImageView, 2> attachments = {
|
---|
| 1186 | swapChainImageViews[i],
|
---|
| 1187 | depthImage.imageView
|
---|
| 1188 | };
|
---|
| 1189 |
|
---|
| 1190 | VkFramebufferCreateInfo framebufferInfo = {};
|
---|
| 1191 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
---|
| 1192 | framebufferInfo.renderPass = renderPass;
|
---|
| 1193 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
| 1194 | framebufferInfo.pAttachments = attachments.data();
|
---|
| 1195 | framebufferInfo.width = swapChainExtent.width;
|
---|
| 1196 | framebufferInfo.height = swapChainExtent.height;
|
---|
| 1197 | framebufferInfo.layers = 1;
|
---|
| 1198 |
|
---|
| 1199 | if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
|
---|
| 1200 | throw runtime_error("failed to create framebuffer!");
|
---|
| 1201 | }
|
---|
| 1202 | }
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
| 1205 | void VulkanGame::createCommandBuffers() {
|
---|
| 1206 | commandBuffers.resize(swapChainImages.size());
|
---|
| 1207 |
|
---|
| 1208 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
| 1209 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
| 1210 | allocInfo.commandPool = commandPool;
|
---|
| 1211 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
| 1212 | allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
|
---|
| 1213 |
|
---|
| 1214 | if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
|
---|
| 1215 | throw runtime_error("failed to allocate command buffers!");
|
---|
| 1216 | }
|
---|
| 1217 |
|
---|
| 1218 | for (size_t i = 0; i < commandBuffers.size(); i++) {
|
---|
| 1219 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
| 1220 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
| 1221 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
---|
| 1222 | beginInfo.pInheritanceInfo = nullptr;
|
---|
| 1223 |
|
---|
| 1224 | if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
|
---|
| 1225 | throw runtime_error("failed to begin recording command buffer!");
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 | VkRenderPassBeginInfo renderPassInfo = {};
|
---|
| 1229 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
---|
| 1230 | renderPassInfo.renderPass = renderPass;
|
---|
| 1231 | renderPassInfo.framebuffer = swapChainFramebuffers[i];
|
---|
| 1232 | renderPassInfo.renderArea.offset = { 0, 0 };
|
---|
| 1233 | renderPassInfo.renderArea.extent = swapChainExtent;
|
---|
| 1234 |
|
---|
| 1235 | array<VkClearValue, 2> clearValues = {};
|
---|
| 1236 | clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
|
---|
| 1237 | clearValues[1].depthStencil = { 1.0f, 0 };
|
---|
| 1238 |
|
---|
| 1239 | renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
---|
| 1240 | renderPassInfo.pClearValues = clearValues.data();
|
---|
| 1241 |
|
---|
| 1242 | vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
---|
| 1243 |
|
---|
[b8777b7] | 1244 | modelPipeline.createRenderCommands(commandBuffers[i], i);
|
---|
[3782d66] | 1245 | shipPipeline.createRenderCommands(commandBuffers[i], i);
|
---|
| 1246 |
|
---|
| 1247 | // Always render this pipeline last
|
---|
[b8777b7] | 1248 | overlayPipeline.createRenderCommands(commandBuffers[i], i);
|
---|
[603b5bc] | 1249 |
|
---|
| 1250 | vkCmdEndRenderPass(commandBuffers[i]);
|
---|
| 1251 |
|
---|
| 1252 | if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
|
---|
| 1253 | throw runtime_error("failed to record command buffer!");
|
---|
| 1254 | }
|
---|
| 1255 | }
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
[34bdf3a] | 1258 | void VulkanGame::createSyncObjects() {
|
---|
| 1259 | imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 1260 | renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 1261 | inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
| 1262 |
|
---|
| 1263 | VkSemaphoreCreateInfo semaphoreInfo = {};
|
---|
| 1264 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
---|
| 1265 |
|
---|
| 1266 | VkFenceCreateInfo fenceInfo = {};
|
---|
| 1267 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
---|
| 1268 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
---|
| 1269 |
|
---|
| 1270 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
| 1271 | if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
|
---|
| 1272 | vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
|
---|
| 1273 | vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
|
---|
| 1274 | throw runtime_error("failed to create synchronization objects for a frame!");
|
---|
| 1275 | }
|
---|
| 1276 | }
|
---|
| 1277 | }
|
---|
| 1278 |
|
---|
[055750a] | 1279 | void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
| 1280 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) {
|
---|
| 1281 | buffers.resize(swapChainImages.size());
|
---|
| 1282 | buffersMemory.resize(swapChainImages.size());
|
---|
| 1283 | bufferInfoList.resize(swapChainImages.size());
|
---|
| 1284 |
|
---|
| 1285 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
| 1286 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags,
|
---|
| 1287 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 1288 | buffers[i], buffersMemory[i]);
|
---|
| 1289 |
|
---|
| 1290 | bufferInfoList[i].buffer = buffers[i];
|
---|
| 1291 | bufferInfoList[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
|
---|
| 1292 | bufferInfoList[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
|
---|
| 1293 | }
|
---|
| 1294 | }
|
---|
| 1295 |
|
---|
[e3bef3a] | 1296 | // TODO: Fix the crash that happens when alt-tabbing
|
---|
[d2d9286] | 1297 | void VulkanGame::recreateSwapChain() {
|
---|
| 1298 | cout << "Recreating swap chain" << endl;
|
---|
| 1299 | gui->refreshWindowSize();
|
---|
| 1300 |
|
---|
| 1301 | while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
|
---|
| 1302 | (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
|
---|
| 1303 | SDL_WaitEvent(nullptr);
|
---|
| 1304 | gui->refreshWindowSize();
|
---|
| 1305 | }
|
---|
| 1306 |
|
---|
| 1307 | vkDeviceWaitIdle(device);
|
---|
| 1308 |
|
---|
[0ae182f] | 1309 | cleanupSwapChain();
|
---|
| 1310 |
|
---|
| 1311 | createSwapChain();
|
---|
| 1312 | createImageViews();
|
---|
| 1313 | createRenderPass();
|
---|
| 1314 |
|
---|
| 1315 | VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
|
---|
| 1316 | depthImage, graphicsQueue);
|
---|
| 1317 | createFramebuffers();
|
---|
[f97c5e7] | 1318 |
|
---|
[055750a] | 1319 | createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
| 1320 | uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
|
---|
| 1321 | createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
---|
| 1322 | storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
|
---|
[0ae182f] | 1323 |
|
---|
[b8777b7] | 1324 | modelPipeline.updateRenderPass(renderPass);
|
---|
| 1325 | modelPipeline.createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
|
---|
| 1326 | modelPipeline.createDescriptorPool(swapChainImages);
|
---|
| 1327 | modelPipeline.createDescriptorSets(swapChainImages);
|
---|
[0ae182f] | 1328 |
|
---|
[b8777b7] | 1329 | overlayPipeline.updateRenderPass(renderPass);
|
---|
| 1330 | overlayPipeline.createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
|
---|
| 1331 | overlayPipeline.createDescriptorPool(swapChainImages);
|
---|
| 1332 | overlayPipeline.createDescriptorSets(swapChainImages);
|
---|
[0ae182f] | 1333 |
|
---|
[055750a] | 1334 | createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
| 1335 | uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
|
---|
| 1336 | createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
---|
| 1337 | storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
|
---|
[3782d66] | 1338 |
|
---|
| 1339 | shipPipeline.updateRenderPass(renderPass);
|
---|
| 1340 | shipPipeline.createPipeline("shaders/ship-vert.spv", "shaders/ship-frag.spv");
|
---|
| 1341 | shipPipeline.createDescriptorPool(swapChainImages);
|
---|
| 1342 | shipPipeline.createDescriptorSets(swapChainImages);
|
---|
| 1343 |
|
---|
[0ae182f] | 1344 | createCommandBuffers();
|
---|
[d2d9286] | 1345 | }
|
---|
| 1346 |
|
---|
[f94eea9] | 1347 | void VulkanGame::cleanupSwapChain() {
|
---|
[603b5bc] | 1348 | VulkanUtils::destroyVulkanImage(device, depthImage);
|
---|
| 1349 |
|
---|
| 1350 | for (VkFramebuffer framebuffer : swapChainFramebuffers) {
|
---|
| 1351 | vkDestroyFramebuffer(device, framebuffer, nullptr);
|
---|
| 1352 | }
|
---|
| 1353 |
|
---|
| 1354 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 1355 |
|
---|
[b8777b7] | 1356 | modelPipeline.cleanup();
|
---|
| 1357 | overlayPipeline.cleanup();
|
---|
[3782d66] | 1358 | shipPipeline.cleanup();
|
---|
[b794178] | 1359 |
|
---|
[6fc24c7] | 1360 | vkDestroyRenderPass(device, renderPass, nullptr);
|
---|
| 1361 |
|
---|
[b794178] | 1362 | for (VkImageView imageView : swapChainImageViews) {
|
---|
[f94eea9] | 1363 | vkDestroyImageView(device, imageView, nullptr);
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 | vkDestroySwapchainKHR(device, swapChain, nullptr);
|
---|
[e83b155] | 1367 |
|
---|
[055750a] | 1368 | for (size_t i = 0; i < uniformBuffers_scenePipeline.size(); i++) {
|
---|
| 1369 | vkDestroyBuffer(device, uniformBuffers_scenePipeline[i], nullptr);
|
---|
| 1370 | vkFreeMemory(device, uniformBuffersMemory_scenePipeline[i], nullptr);
|
---|
| 1371 | }
|
---|
| 1372 |
|
---|
| 1373 | for (size_t i = 0; i < storageBuffers_scenePipeline.size(); i++) {
|
---|
| 1374 | vkDestroyBuffer(device, storageBuffers_scenePipeline[i], nullptr);
|
---|
| 1375 | vkFreeMemory(device, storageBuffersMemory_scenePipeline[i], nullptr);
|
---|
[e83b155] | 1376 | }
|
---|
[3782d66] | 1377 |
|
---|
| 1378 | for (size_t i = 0; i < uniformBuffers_shipPipeline.size(); i++) {
|
---|
| 1379 | vkDestroyBuffer(device, uniformBuffers_shipPipeline[i], nullptr);
|
---|
| 1380 | vkFreeMemory(device, uniformBuffersMemory_shipPipeline[i], nullptr);
|
---|
| 1381 | }
|
---|
[055750a] | 1382 |
|
---|
| 1383 | for (size_t i = 0; i < storageBuffers_shipPipeline.size(); i++) {
|
---|
| 1384 | vkDestroyBuffer(device, storageBuffers_shipPipeline[i], nullptr);
|
---|
| 1385 | vkFreeMemory(device, storageBuffersMemory_shipPipeline[i], nullptr);
|
---|
| 1386 | }
|
---|
[cc4a8b5] | 1387 | }
|
---|