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