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