[99d44b2] | 1 | #ifndef _VULKAN_GAME_H
|
---|
| 2 | #define _VULKAN_GAME_H
|
---|
[e8ebc76] | 3 |
|
---|
[aa7707d] | 4 | #include <algorithm>
|
---|
[0807aeb] | 5 | #include <chrono>
|
---|
[e1f88a9] | 6 | #include <map>
|
---|
[0807aeb] | 7 |
|
---|
[60578ce] | 8 | #define GLM_FORCE_RADIANS
|
---|
| 9 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
[a79be34] | 10 | #define GLM_FORCE_RIGHT_HANDED
|
---|
[60578ce] | 11 |
|
---|
[771b33a] | 12 | #include <glm/glm.hpp>
|
---|
[15104a8] | 13 | #include <glm/gtc/matrix_transform.hpp>
|
---|
[771b33a] | 14 |
|
---|
[6bfd91c] | 15 | #include <vulkan/vulkan.h>
|
---|
| 16 |
|
---|
[c324d6a] | 17 | #include <SDL2/SDL.h>
|
---|
[6bfd91c] | 18 | #include <SDL2/SDL_ttf.h>
|
---|
| 19 |
|
---|
[ea2b4dc] | 20 | #include "IMGUI/imgui_impl_vulkan.h"
|
---|
| 21 |
|
---|
[6bfd91c] | 22 | #include "consts.hpp"
|
---|
[4e705d6] | 23 | #include "vulkan-utils.hpp"
|
---|
[7d2b0b9] | 24 | #include "graphics-pipeline_vulkan.hpp"
|
---|
[0df3c9a] | 25 |
|
---|
[4e705d6] | 26 | #include "game-gui-sdl.hpp"
|
---|
[b794178] | 27 |
|
---|
[e1f88a9] | 28 | #include "gui/screen.hpp"
|
---|
| 29 |
|
---|
[15104a8] | 30 | using namespace glm;
|
---|
[0807aeb] | 31 | using namespace std::chrono;
|
---|
[15104a8] | 32 |
|
---|
[2e77b3f] | 33 | #ifdef NDEBUG
|
---|
| 34 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
| 35 | #else
|
---|
| 36 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
[5a1ace0] | 39 | struct OverlayVertex {
|
---|
[15104a8] | 40 | vec3 pos;
|
---|
| 41 | vec2 texCoord;
|
---|
[771b33a] | 42 | };
|
---|
| 43 |
|
---|
[5a1ace0] | 44 | struct ModelVertex {
|
---|
[15104a8] | 45 | vec3 pos;
|
---|
[5a1ace0] | 46 | vec3 color;
|
---|
[15104a8] | 47 | vec2 texCoord;
|
---|
[5a1ace0] | 48 | unsigned int objIndex;
|
---|
[15104a8] | 49 | };
|
---|
| 50 |
|
---|
[3782d66] | 51 | struct ShipVertex {
|
---|
| 52 | vec3 pos;
|
---|
| 53 | vec3 color;
|
---|
[06d959f] | 54 | vec3 normal;
|
---|
[cf727ca] | 55 | unsigned int objIndex;
|
---|
[3782d66] | 56 | };
|
---|
| 57 |
|
---|
[3e8cc8b] | 58 | struct AsteroidVertex {
|
---|
| 59 | vec3 pos;
|
---|
| 60 | vec3 color;
|
---|
| 61 | vec3 normal;
|
---|
| 62 | unsigned int objIndex;
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[237cbec] | 65 | struct LaserVertex {
|
---|
| 66 | vec3 pos;
|
---|
| 67 | vec2 texCoord;
|
---|
| 68 | unsigned int objIndex;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
[4a9416a] | 71 | struct ExplosionVertex {
|
---|
| 72 | vec3 particleStartVelocity;
|
---|
| 73 | float particleStartTime;
|
---|
| 74 | unsigned int objIndex;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[2d87297] | 77 | struct SSBO_ModelObject {
|
---|
[055750a] | 78 | alignas(16) mat4 model;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[2d87297] | 81 | struct SSBO_Asteroid {
|
---|
[3e8cc8b] | 82 | alignas(16) mat4 model;
|
---|
| 83 | alignas(4) float hp;
|
---|
[4ece3bf] | 84 | alignas(4) unsigned int deleted;
|
---|
[3e8cc8b] | 85 | };
|
---|
| 86 |
|
---|
[237cbec] | 87 | struct SSBO_Laser {
|
---|
| 88 | alignas(16) mat4 model;
|
---|
| 89 | alignas(4) vec3 color;
|
---|
| 90 | alignas(4) unsigned int deleted;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
[4a9416a] | 93 | struct SSBO_Explosion {
|
---|
| 94 | alignas(16) mat4 model;
|
---|
| 95 | alignas(4) float explosionStartTime;
|
---|
| 96 | alignas(4) float explosionDuration;
|
---|
| 97 | alignas(4) unsigned int deleted;
|
---|
| 98 | };
|
---|
| 99 |
|
---|
[52a02e6] | 100 | struct UBO_VP_mats {
|
---|
| 101 | alignas(16) mat4 view;
|
---|
| 102 | alignas(16) mat4 proj;
|
---|
| 103 | };
|
---|
| 104 |
|
---|
[4a9416a] | 105 | struct UBO_Explosion {
|
---|
| 106 | alignas(16) mat4 view;
|
---|
| 107 | alignas(16) mat4 proj;
|
---|
| 108 | alignas(4) float cur_time;
|
---|
| 109 | };
|
---|
| 110 |
|
---|
[4994692] | 111 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
| 112 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
| 113 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
| 114 | template<class VertexType, class SSBOType>
|
---|
| 115 | struct SceneObject {
|
---|
| 116 | vector<VertexType> vertices;
|
---|
| 117 | vector<uint16_t> indices;
|
---|
| 118 | SSBOType ssbo;
|
---|
| 119 |
|
---|
| 120 | mat4 model_base;
|
---|
| 121 | mat4 model_transform;
|
---|
| 122 |
|
---|
[5ba732a] | 123 | bool modified;
|
---|
| 124 |
|
---|
[4994692] | 125 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
| 126 | // parent class
|
---|
| 127 | vec3 center; // currently only matters for asteroids
|
---|
| 128 | float radius; // currently only matters for asteroids
|
---|
[3950236] | 129 | SceneObject<AsteroidVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
---|
[4994692] | 130 | };
|
---|
| 131 |
|
---|
| 132 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
[2da64ef] | 133 | // Could probably use the same approach to make indices optional
|
---|
[4994692] | 134 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
| 135 | // them mamdatory
|
---|
[2da64ef] | 136 |
|
---|
[6104594] | 137 | // TODO: Make a singleton timer class instead
|
---|
| 138 | static float curTime;
|
---|
| 139 |
|
---|
[7297892] | 140 |
|
---|
| 141 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
---|
| 142 |
|
---|
| 143 | struct BaseEffectOverTime {
|
---|
| 144 | bool deleted;
|
---|
| 145 |
|
---|
| 146 | virtual void applyEffect() = 0;
|
---|
| 147 |
|
---|
| 148 | BaseEffectOverTime() :
|
---|
| 149 | deleted(false) {
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | virtual ~BaseEffectOverTime() {
|
---|
| 153 | }
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 | template<class VertexType, class SSBOType>
|
---|
| 157 | struct EffectOverTime : public BaseEffectOverTime {
|
---|
| 158 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline;
|
---|
| 159 | vector<SceneObject<VertexType, SSBOType>>& objects;
|
---|
| 160 | unsigned int objectIndex;
|
---|
| 161 | size_t effectedFieldOffset;
|
---|
| 162 | float startValue;
|
---|
| 163 | float startTime;
|
---|
| 164 | float changePerSecond;
|
---|
| 165 |
|
---|
| 166 | EffectOverTime(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 167 | vector<SceneObject<VertexType, SSBOType>>& objects, unsigned int objectIndex,
|
---|
| 168 | size_t effectedFieldOffset, float changePerSecond) :
|
---|
| 169 | pipeline(pipeline),
|
---|
| 170 | objects(objects),
|
---|
| 171 | objectIndex(objectIndex),
|
---|
| 172 | effectedFieldOffset(effectedFieldOffset),
|
---|
| 173 | startTime(curTime),
|
---|
| 174 | changePerSecond(changePerSecond) {
|
---|
| 175 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 176 |
|
---|
| 177 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 178 | ssboOffset + effectedFieldOffset;
|
---|
| 179 |
|
---|
| 180 | startValue = *reinterpret_cast<float*>(effectedFieldPtr);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void applyEffect() {
|
---|
| 184 | if (objects[objectIndex].ssbo.deleted) {
|
---|
| 185 | this->deleted = true;
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 190 |
|
---|
| 191 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 192 | ssboOffset + effectedFieldOffset;
|
---|
| 193 |
|
---|
| 194 | *reinterpret_cast<float*>(effectedFieldPtr) = startValue + (curTime - startTime) * changePerSecond;
|
---|
| 195 |
|
---|
| 196 | objects[objectIndex].modified = true;
|
---|
| 197 | }
|
---|
| 198 | };
|
---|
| 199 |
|
---|
[99d44b2] | 200 | class VulkanGame {
|
---|
[e8ebc76] | 201 | public:
|
---|
[3f32dfd] | 202 | VulkanGame();
|
---|
[99d44b2] | 203 | ~VulkanGame();
|
---|
[0df3c9a] | 204 |
|
---|
[b6e60b4] | 205 | void run(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 206 |
|
---|
[e1f88a9] | 207 | void goToScreen(Screen* screen);
|
---|
| 208 | void quitGame();
|
---|
| 209 |
|
---|
| 210 | map<ScreenType, Screen*> screens;
|
---|
| 211 | Screen* currentScreen;
|
---|
| 212 |
|
---|
[6bfd91c] | 213 | TTF_Font* lazyFont;
|
---|
[e1f88a9] | 214 | TTF_Font* proggyFont;
|
---|
| 215 |
|
---|
[f809ae6] | 216 | int score;
|
---|
| 217 | float fps;
|
---|
| 218 |
|
---|
[4e705d6] | 219 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
---|
| 220 |
|
---|
| 221 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
| 222 |
|
---|
| 223 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
| 224 |
|
---|
| 225 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
| 226 |
|
---|
| 227 | GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser> laserPipeline;
|
---|
| 228 |
|
---|
| 229 | GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline;
|
---|
| 230 |
|
---|
[0df3c9a] | 231 | private:
|
---|
[c324d6a] | 232 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 233 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 234 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 235 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 236 | void* pUserData);
|
---|
| 237 |
|
---|
[5ab1b20] | 238 | const float NEAR_CLIP = 0.1f;
|
---|
| 239 | const float FAR_CLIP = 100.0f;
|
---|
[60578ce] | 240 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
[5ab1b20] | 241 |
|
---|
[4a9416a] | 242 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
---|
| 243 | const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
|
---|
| 244 |
|
---|
[c6f0793] | 245 | bool done;
|
---|
[e1f88a9] | 246 |
|
---|
[15104a8] | 247 | vec3 cam_pos;
|
---|
| 248 |
|
---|
[4e705d6] | 249 | // TODO: Good place to start using smart pointers
|
---|
[0df3c9a] | 250 | GameGui* gui;
|
---|
[c559904] | 251 |
|
---|
| 252 | SDL_version sdlVersion;
|
---|
[b794178] | 253 | SDL_Window* window = nullptr;
|
---|
| 254 | SDL_Renderer* renderer = nullptr;
|
---|
| 255 |
|
---|
| 256 | SDL_Texture* uiOverlay = nullptr;
|
---|
[c1d9b2a] | 257 |
|
---|
| 258 | VkInstance instance;
|
---|
| 259 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
[fe5c3ba] | 260 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
[90a424f] | 261 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
[c1c2021] | 262 | VkDevice device;
|
---|
| 263 |
|
---|
| 264 | VkQueue graphicsQueue;
|
---|
| 265 | VkQueue presentQueue;
|
---|
[0df3c9a] | 266 |
|
---|
[3f32dfd] | 267 | // TODO: Maybe make a swapchain struct for convenience
|
---|
| 268 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
---|
| 269 | VkPresentModeKHR swapChainPresentMode;
|
---|
| 270 | VkExtent2D swapChainExtent;
|
---|
| 271 | uint32_t swapChainMinImageCount;
|
---|
[c324d6a] | 272 | uint32_t swapChainImageCount;
|
---|
[502bd0b] | 273 | VkSwapchainKHR swapChain;
|
---|
| 274 | vector<VkImage> swapChainImages;
|
---|
[f94eea9] | 275 | vector<VkImageView> swapChainImageViews;
|
---|
[603b5bc] | 276 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
[fa9fa1c] | 277 |
|
---|
[6fc24c7] | 278 | VkRenderPass renderPass;
|
---|
[3f32dfd] | 279 |
|
---|
| 280 | VkCommandPool resourceCommandPool;
|
---|
| 281 |
|
---|
[9c0a614] | 282 | vector<VkCommandPool> commandPools;
|
---|
[603b5bc] | 283 | vector<VkCommandBuffer> commandBuffers;
|
---|
[502bd0b] | 284 |
|
---|
[603b5bc] | 285 | VulkanImage depthImage;
|
---|
[b794178] | 286 |
|
---|
[3f32dfd] | 287 | // These are per frame
|
---|
| 288 | vector<VkSemaphore> imageAcquiredSemaphores;
|
---|
| 289 | vector<VkSemaphore> renderCompleteSemaphores;
|
---|
| 290 |
|
---|
| 291 | // These are per swap chain image
|
---|
[4e705d6] | 292 | vector<VkFence> inFlightFences;
|
---|
| 293 |
|
---|
[3f32dfd] | 294 | uint32_t imageIndex;
|
---|
| 295 | uint32_t currentFrame;
|
---|
[4e705d6] | 296 |
|
---|
[28ea92f] | 297 | bool shouldRecreateSwapChain;
|
---|
[4e705d6] | 298 |
|
---|
[3b7d497] | 299 | VkDescriptorPool imguiDescriptorPool;
|
---|
| 300 |
|
---|
[b794178] | 301 | VkSampler textureSampler;
|
---|
| 302 |
|
---|
[0fe8433] | 303 | VulkanImage sdlOverlayImage;
|
---|
| 304 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
| 305 |
|
---|
[4994692] | 306 | VulkanImage floorTextureImage;
|
---|
| 307 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 308 |
|
---|
[237cbec] | 309 | VulkanImage laserTextureImage;
|
---|
| 310 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
---|
| 311 |
|
---|
[22217d4] | 312 | mat4 viewMat, projMat;
|
---|
| 313 |
|
---|
[860a0da] | 314 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
| 315 | // per pipeline.
|
---|
| 316 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
| 317 | // the objects vector, the ubo, and the ssbo
|
---|
| 318 |
|
---|
[2ba5617] | 319 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
| 320 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
| 321 |
|
---|
[4e705d6] | 322 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
---|
| 323 |
|
---|
[2d87297] | 324 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
[0fe8433] | 325 |
|
---|
[d25381b] | 326 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
| 327 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
| 328 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
[b794178] | 329 |
|
---|
[0fe8433] | 330 | UBO_VP_mats object_VP_mats;
|
---|
| 331 |
|
---|
[2d87297] | 332 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
[0fe8433] | 333 |
|
---|
[3782d66] | 334 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
| 335 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
| 336 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
| 337 |
|
---|
[0fe8433] | 338 | UBO_VP_mats ship_VP_mats;
|
---|
[0e09340] | 339 |
|
---|
[2d87297] | 340 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
[3e8cc8b] | 341 |
|
---|
| 342 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
| 343 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
| 344 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
| 345 |
|
---|
| 346 | UBO_VP_mats asteroid_VP_mats;
|
---|
| 347 |
|
---|
[237cbec] | 348 | vector<SceneObject<LaserVertex, SSBO_Laser>> laserObjects;
|
---|
| 349 |
|
---|
| 350 | vector<VkBuffer> uniformBuffers_laserPipeline;
|
---|
| 351 | vector<VkDeviceMemory> uniformBuffersMemory_laserPipeline;
|
---|
| 352 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_laserPipeline;
|
---|
| 353 |
|
---|
| 354 | UBO_VP_mats laser_VP_mats;
|
---|
| 355 |
|
---|
[4a9416a] | 356 | vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
|
---|
| 357 |
|
---|
| 358 | vector<VkBuffer> uniformBuffers_explosionPipeline;
|
---|
| 359 | vector<VkDeviceMemory> uniformBuffersMemory_explosionPipeline;
|
---|
| 360 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_explosionPipeline;
|
---|
| 361 |
|
---|
| 362 | UBO_Explosion explosion_UBO;
|
---|
| 363 |
|
---|
[7297892] | 364 | vector<BaseEffectOverTime*> effects;
|
---|
| 365 |
|
---|
[0807aeb] | 366 | time_point<steady_clock> startTime;
|
---|
[6104594] | 367 | float prevTime, elapsedTime;
|
---|
[0807aeb] | 368 |
|
---|
[f809ae6] | 369 | float fpsStartTime;
|
---|
| 370 | int frameCount;
|
---|
| 371 |
|
---|
[0807aeb] | 372 | float shipSpeed = 0.5f;
|
---|
| 373 | float asteroidSpeed = 2.0f;
|
---|
| 374 |
|
---|
| 375 | float spawnRate_asteroid = 0.5;
|
---|
| 376 | float lastSpawn_asteroid;
|
---|
[4ece3bf] | 377 |
|
---|
[1f81ecc] | 378 | unsigned int leftLaserIdx = -1;
|
---|
[7297892] | 379 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
---|
[1f81ecc] | 380 |
|
---|
| 381 | unsigned int rightLaserIdx = -1;
|
---|
[7297892] | 382 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
---|
[1f81ecc] | 383 |
|
---|
[4e705d6] | 384 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 385 | void initVulkan();
|
---|
[f97c5e7] | 386 | void initGraphicsPipelines();
|
---|
[15104a8] | 387 | void initMatrices();
|
---|
[0df3c9a] | 388 | void mainLoop();
|
---|
[3f32dfd] | 389 | void updateScene();
|
---|
[0df3c9a] | 390 | void cleanup();
|
---|
[c1d9b2a] | 391 |
|
---|
[c324d6a] | 392 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
[c1d9b2a] | 393 | void setupDebugMessenger();
|
---|
| 394 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
[90a424f] | 395 | void createVulkanSurface();
|
---|
[fe5c3ba] | 396 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[fa9fa1c] | 397 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
[c324d6a] | 398 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
[c1c2021] | 399 | const vector<const char*>& deviceExtensions);
|
---|
[3f32dfd] | 400 | void chooseSwapChainProperties();
|
---|
[502bd0b] | 401 | void createSwapChain();
|
---|
[f94eea9] | 402 | void createImageViews();
|
---|
[6fc24c7] | 403 | void createRenderPass();
|
---|
[3f32dfd] | 404 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
| 405 | void createResourceCommandPool();
|
---|
[9c0a614] | 406 | void createCommandPools();
|
---|
[603b5bc] | 407 | void createImageResources();
|
---|
| 408 | void createFramebuffers();
|
---|
| 409 | void createCommandBuffers();
|
---|
[34bdf3a] | 410 | void createSyncObjects();
|
---|
[f94eea9] | 411 |
|
---|
[3f32dfd] | 412 | void createTextureSampler();
|
---|
| 413 |
|
---|
[3b7d497] | 414 | void createImguiDescriptorPool();
|
---|
| 415 | void destroyImguiDescriptorPool();
|
---|
| 416 |
|
---|
[4994692] | 417 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
| 418 | // stop using objects.back() to access the object that was just created
|
---|
[2d87297] | 419 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 420 | SceneObject<VertexType, SSBOType>& addObject(
|
---|
| 421 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 422 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 423 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 424 | bool pipelinesCreated);
|
---|
[0fe8433] | 425 |
|
---|
[2da64ef] | 426 | template<class VertexType, class SSBOType>
|
---|
| 427 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[4994692] | 428 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
[2da64ef] | 429 |
|
---|
[1f81ecc] | 430 | template<class VertexType, class SSBOType>
|
---|
| 431 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 432 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
---|
| 433 |
|
---|
[06d959f] | 434 | template<class VertexType>
|
---|
| 435 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 436 |
|
---|
[cf727ca] | 437 | template<class VertexType>
|
---|
| 438 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 439 |
|
---|
[3b84bb6] | 440 | template<class VertexType, class SSBOType>
|
---|
| 441 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
[a79be34] | 442 |
|
---|
[52a02e6] | 443 | void addLaser(vec3 start, vec3 end, vec3 color, float width);
|
---|
| 444 | void translateLaser(size_t index, const vec3& translation);
|
---|
| 445 | void updateLaserTarget(size_t index);
|
---|
| 446 | bool getLaserAndAsteroidIntersection(SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid,
|
---|
| 447 | vec3& start, vec3& end, vec3& intersection);
|
---|
| 448 |
|
---|
[4a9416a] | 449 | void addExplosion(mat4 model_mat, float duration, float cur_time);
|
---|
| 450 |
|
---|
[055750a] | 451 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
[4994692] | 452 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
| 453 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
[f97c5e7] | 454 |
|
---|
[ea2b4dc] | 455 | void renderFrame(ImDrawData* draw_data);
|
---|
[4e2c709] | 456 | void presentFrame();
|
---|
| 457 |
|
---|
[d2d9286] | 458 | void recreateSwapChain();
|
---|
| 459 |
|
---|
[c1c2021] | 460 | void cleanupSwapChain();
|
---|
[e8ebc76] | 461 | };
|
---|
| 462 |
|
---|
[4a9416a] | 463 | // Start of specialized no-op functions
|
---|
| 464 |
|
---|
| 465 | template<>
|
---|
| 466 | inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | // End of specialized no-op functions
|
---|
| 470 |
|
---|
[3b84bb6] | 471 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
| 472 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
| 473 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
---|
[2ba5617] | 474 |
|
---|
| 475 | // TODO: Maybe return a reference to the object from this method if I decide that updating it
|
---|
| 476 | // immediately after creation is a good idea (such as setting model_base)
|
---|
| 477 | // Currently, model_base is set like this in a few places and the radius is set for asteroids
|
---|
| 478 | // to account for scaling
|
---|
[2d87297] | 479 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 480 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
| 481 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[2d87297] | 482 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
[3b84bb6] | 483 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 484 | bool pipelinesCreated) {
|
---|
[2ba5617] | 485 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
| 486 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
[0fe8433] | 487 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 488 |
|
---|
| 489 | for (uint16_t& idx : indices) {
|
---|
| 490 | idx += numVertices;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[5ba732a] | 493 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
---|
[3b84bb6] | 494 |
|
---|
[2ba5617] | 495 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
[1f81ecc] | 496 |
|
---|
[4a9416a] | 497 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
[1f81ecc] | 498 | centerObject(obj);
|
---|
| 499 | }
|
---|
[2ba5617] | 500 |
|
---|
[4994692] | 501 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
[9067efc] | 502 | resourceCommandPool, graphicsQueue);
|
---|
[0fe8433] | 503 |
|
---|
[3b84bb6] | 504 | if (pipelinesCreated) {
|
---|
[44f23af] | 505 | vkDeviceWaitIdle(device);
|
---|
[9c0a614] | 506 |
|
---|
| 507 | for (uint32_t i = 0; i < swapChainImageCount; i++) {
|
---|
| 508 | vkFreeCommandBuffers(device, commandPools[i], 1, &commandBuffers[i]);
|
---|
| 509 | }
|
---|
[44f23af] | 510 |
|
---|
| 511 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
---|
| 512 | // one SSBO is resized.
|
---|
| 513 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
---|
| 514 | // are created and then recreate each of the corresponding pipelines only once per frame
|
---|
[3b84bb6] | 515 | if (storageBufferResized) {
|
---|
[44f23af] | 516 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
| 517 | pipeline.createDescriptorPool(swapChainImages);
|
---|
| 518 | pipeline.createDescriptorSets(swapChainImages);
|
---|
[3b84bb6] | 519 | }
|
---|
| 520 |
|
---|
| 521 | createCommandBuffers();
|
---|
| 522 | }
|
---|
[4994692] | 523 |
|
---|
| 524 | return obj;
|
---|
[0fe8433] | 525 | }
|
---|
| 526 |
|
---|
[0807aeb] | 527 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
[2da64ef] | 528 | template<class VertexType, class SSBOType>
|
---|
| 529 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 530 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
---|
| 531 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
---|
| 532 |
|
---|
| 533 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
[2ba5617] | 534 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[2da64ef] | 535 |
|
---|
| 536 | pipeline.updateObject(index, obj.ssbo);
|
---|
[5ba732a] | 537 |
|
---|
| 538 | obj.modified = false;
|
---|
[2da64ef] | 539 | }
|
---|
| 540 |
|
---|
[1f81ecc] | 541 | template<class VertexType, class SSBOType>
|
---|
| 542 | void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 543 | SceneObject<VertexType, SSBOType>& obj, size_t index) {
|
---|
[9067efc] | 544 | pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
|
---|
[1f81ecc] | 545 | }
|
---|
| 546 |
|
---|
[06d959f] | 547 | template<class VertexType>
|
---|
| 548 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 549 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 550 | vec3 p1 = vertices[i].pos;
|
---|
| 551 | vec3 p2 = vertices[i+1].pos;
|
---|
| 552 | vec3 p3 = vertices[i+2].pos;
|
---|
| 553 |
|
---|
[a79be34] | 554 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
[06d959f] | 555 |
|
---|
| 556 | // Add the same normal for all 3 vertices
|
---|
| 557 | vertices[i].normal = normal;
|
---|
| 558 | vertices[i+1].normal = normal;
|
---|
| 559 | vertices[i+2].normal = normal;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | return vertices;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
[cf727ca] | 565 | template<class VertexType>
|
---|
| 566 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 567 | for (VertexType& vertex : vertices) {
|
---|
| 568 | vertex.objIndex = objIndex;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | return vertices;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[3b84bb6] | 574 | template<class VertexType, class SSBOType>
|
---|
| 575 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
| 576 | vector<VertexType>& vertices = object.vertices;
|
---|
| 577 |
|
---|
[a79be34] | 578 | float min_x = vertices[0].pos.x;
|
---|
| 579 | float max_x = vertices[0].pos.x;
|
---|
| 580 | float min_y = vertices[0].pos.y;
|
---|
| 581 | float max_y = vertices[0].pos.y;
|
---|
| 582 | float min_z = vertices[0].pos.z;
|
---|
| 583 | float max_z = vertices[0].pos.z;
|
---|
| 584 |
|
---|
| 585 | // start from the second point
|
---|
| 586 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
[3b84bb6] | 587 | vec3& pos = vertices[i].pos;
|
---|
| 588 |
|
---|
| 589 | if (min_x > pos.x) {
|
---|
| 590 | min_x = pos.x;
|
---|
| 591 | } else if (max_x < pos.x) {
|
---|
| 592 | max_x = pos.x;
|
---|
[a79be34] | 593 | }
|
---|
| 594 |
|
---|
[3b84bb6] | 595 | if (min_y > pos.y) {
|
---|
| 596 | min_y = pos.y;
|
---|
| 597 | } else if (max_y < pos.y) {
|
---|
| 598 | max_y = pos.y;
|
---|
[a79be34] | 599 | }
|
---|
| 600 |
|
---|
[3b84bb6] | 601 | if (min_z > pos.z) {
|
---|
| 602 | min_z = pos.z;
|
---|
| 603 | } else if (max_z < pos.z) {
|
---|
| 604 | max_z = pos.z;
|
---|
[a79be34] | 605 | }
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 609 |
|
---|
| 610 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 611 | vertices[i].pos -= center;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[2ba5617] | 614 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
| 615 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
| 616 |
|
---|
[3b84bb6] | 617 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
[a79be34] | 618 | }
|
---|
| 619 |
|
---|
[3b84bb6] | 620 | #endif // _VULKAN_GAME_H
|
---|