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