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