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