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