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