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