[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"
|
---|
[4e705d6] | 24 | #include "vulkan-utils.hpp"
|
---|
[7d2b0b9] | 25 | #include "graphics-pipeline_vulkan.hpp"
|
---|
[4e705d6] | 26 | #include "game-gui-sdl.hpp"
|
---|
[b794178] | 27 |
|
---|
[15104a8] | 28 | using namespace glm;
|
---|
[0807aeb] | 29 | using namespace std::chrono;
|
---|
[15104a8] | 30 |
|
---|
[2e77b3f] | 31 | #ifdef NDEBUG
|
---|
| 32 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
| 33 | #else
|
---|
| 34 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
[5a1ace0] | 37 | struct OverlayVertex {
|
---|
[15104a8] | 38 | vec3 pos;
|
---|
| 39 | vec2 texCoord;
|
---|
[771b33a] | 40 | };
|
---|
| 41 |
|
---|
[5a1ace0] | 42 | struct ModelVertex {
|
---|
[15104a8] | 43 | vec3 pos;
|
---|
[5a1ace0] | 44 | vec3 color;
|
---|
[15104a8] | 45 | vec2 texCoord;
|
---|
[5a1ace0] | 46 | unsigned int objIndex;
|
---|
[15104a8] | 47 | };
|
---|
| 48 |
|
---|
[3782d66] | 49 | struct ShipVertex {
|
---|
| 50 | vec3 pos;
|
---|
| 51 | vec3 color;
|
---|
[06d959f] | 52 | vec3 normal;
|
---|
[cf727ca] | 53 | unsigned int objIndex;
|
---|
[3782d66] | 54 | };
|
---|
| 55 |
|
---|
[3e8cc8b] | 56 | struct AsteroidVertex {
|
---|
| 57 | vec3 pos;
|
---|
| 58 | vec3 color;
|
---|
| 59 | vec3 normal;
|
---|
| 60 | unsigned int objIndex;
|
---|
| 61 | };
|
---|
| 62 |
|
---|
[237cbec] | 63 | struct LaserVertex {
|
---|
| 64 | vec3 pos;
|
---|
| 65 | vec2 texCoord;
|
---|
| 66 | unsigned int objIndex;
|
---|
| 67 | };
|
---|
| 68 |
|
---|
[4a9416a] | 69 | struct ExplosionVertex {
|
---|
| 70 | vec3 particleStartVelocity;
|
---|
| 71 | float particleStartTime;
|
---|
| 72 | unsigned int objIndex;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
[2d87297] | 75 | struct SSBO_ModelObject {
|
---|
[055750a] | 76 | alignas(16) mat4 model;
|
---|
| 77 | };
|
---|
| 78 |
|
---|
[2d87297] | 79 | struct SSBO_Asteroid {
|
---|
[3e8cc8b] | 80 | alignas(16) mat4 model;
|
---|
| 81 | alignas(4) float hp;
|
---|
[4ece3bf] | 82 | alignas(4) unsigned int deleted;
|
---|
[3e8cc8b] | 83 | };
|
---|
| 84 |
|
---|
[237cbec] | 85 | struct SSBO_Laser {
|
---|
| 86 | alignas(16) mat4 model;
|
---|
| 87 | alignas(4) vec3 color;
|
---|
| 88 | alignas(4) unsigned int deleted;
|
---|
| 89 | };
|
---|
| 90 |
|
---|
[4a9416a] | 91 | struct SSBO_Explosion {
|
---|
| 92 | alignas(16) mat4 model;
|
---|
| 93 | alignas(4) float explosionStartTime;
|
---|
| 94 | alignas(4) float explosionDuration;
|
---|
| 95 | alignas(4) unsigned int deleted;
|
---|
| 96 | };
|
---|
| 97 |
|
---|
[52a02e6] | 98 | struct UBO_VP_mats {
|
---|
| 99 | alignas(16) mat4 view;
|
---|
| 100 | alignas(16) mat4 proj;
|
---|
| 101 | };
|
---|
| 102 |
|
---|
[4a9416a] | 103 | struct UBO_Explosion {
|
---|
| 104 | alignas(16) mat4 view;
|
---|
| 105 | alignas(16) mat4 proj;
|
---|
| 106 | alignas(4) float cur_time;
|
---|
| 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
|
---|
[3950236] | 127 | SceneObject<AsteroidVertex, 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 {
|
---|
| 153 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline;
|
---|
| 154 | vector<SceneObject<VertexType, SSBOType>>& objects;
|
---|
| 155 | unsigned int objectIndex;
|
---|
| 156 | size_t effectedFieldOffset;
|
---|
| 157 | float startValue;
|
---|
| 158 | float startTime;
|
---|
| 159 | float changePerSecond;
|
---|
| 160 |
|
---|
| 161 | EffectOverTime(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 162 | vector<SceneObject<VertexType, SSBOType>>& objects, unsigned int objectIndex,
|
---|
[5192672] | 163 | size_t effectedFieldOffset, float startTime, float changePerSecond) :
|
---|
[7297892] | 164 | pipeline(pipeline),
|
---|
| 165 | objects(objects),
|
---|
| 166 | objectIndex(objectIndex),
|
---|
| 167 | effectedFieldOffset(effectedFieldOffset),
|
---|
[5192672] | 168 | startTime(startTime),
|
---|
[7297892] | 169 | changePerSecond(changePerSecond) {
|
---|
| 170 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 171 |
|
---|
| 172 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 173 | ssboOffset + effectedFieldOffset;
|
---|
| 174 |
|
---|
| 175 | startValue = *reinterpret_cast<float*>(effectedFieldPtr);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[5192672] | 178 | void applyEffect(float curTime) {
|
---|
[7297892] | 179 | if (objects[objectIndex].ssbo.deleted) {
|
---|
| 180 | this->deleted = true;
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 185 |
|
---|
| 186 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 187 | ssboOffset + effectedFieldOffset;
|
---|
| 188 |
|
---|
| 189 | *reinterpret_cast<float*>(effectedFieldPtr) = startValue + (curTime - startTime) * changePerSecond;
|
---|
| 190 |
|
---|
| 191 | objects[objectIndex].modified = true;
|
---|
| 192 | }
|
---|
| 193 | };
|
---|
| 194 |
|
---|
[20e4c2b] | 195 | enum UIValueType {
|
---|
| 196 | UIVALUE_INT,
|
---|
| 197 | UIVALUE_DOUBLE,
|
---|
| 198 | };
|
---|
| 199 |
|
---|
| 200 | struct UIValue {
|
---|
| 201 | UIValueType type;
|
---|
| 202 | string label;
|
---|
| 203 | void* value;
|
---|
| 204 |
|
---|
| 205 | UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
|
---|
| 206 | };
|
---|
| 207 |
|
---|
[99d44b2] | 208 | class VulkanGame {
|
---|
[e8ebc76] | 209 | public:
|
---|
[3f32dfd] | 210 | VulkanGame();
|
---|
[99d44b2] | 211 | ~VulkanGame();
|
---|
[0df3c9a] | 212 |
|
---|
[b6e60b4] | 213 | void run(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 214 |
|
---|
[4e705d6] | 215 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
| 216 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
| 217 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
| 218 | GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser> laserPipeline;
|
---|
| 219 | GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline;
|
---|
| 220 |
|
---|
[0df3c9a] | 221 | private:
|
---|
[c324d6a] | 222 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 223 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 224 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 225 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 226 | void* pUserData);
|
---|
| 227 |
|
---|
[5ab1b20] | 228 | const float NEAR_CLIP = 0.1f;
|
---|
| 229 | const float FAR_CLIP = 100.0f;
|
---|
[60578ce] | 230 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
[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;
|
---|
[fe5c3ba] | 249 | VkSurfaceKHR surface; // TODO: Change the variable name to 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 |
|
---|
[3b7d497] | 288 | VkDescriptorPool imguiDescriptorPool;
|
---|
| 289 |
|
---|
[b794178] | 290 | VkSampler textureSampler;
|
---|
| 291 |
|
---|
[4994692] | 292 | VulkanImage floorTextureImage;
|
---|
| 293 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 294 |
|
---|
[237cbec] | 295 | VulkanImage laserTextureImage;
|
---|
| 296 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
---|
| 297 |
|
---|
[22217d4] | 298 | mat4 viewMat, projMat;
|
---|
| 299 |
|
---|
[860a0da] | 300 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
| 301 | // per pipeline.
|
---|
| 302 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
| 303 | // the objects vector, the ubo, and the ssbo
|
---|
| 304 |
|
---|
[2ba5617] | 305 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
| 306 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
| 307 |
|
---|
[2d87297] | 308 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
[0fe8433] | 309 |
|
---|
[d25381b] | 310 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
| 311 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
| 312 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
[b794178] | 313 |
|
---|
[0fe8433] | 314 | UBO_VP_mats object_VP_mats;
|
---|
| 315 |
|
---|
[2d87297] | 316 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
[0fe8433] | 317 |
|
---|
[3782d66] | 318 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
| 319 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
| 320 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
| 321 |
|
---|
[0fe8433] | 322 | UBO_VP_mats ship_VP_mats;
|
---|
[0e09340] | 323 |
|
---|
[2d87297] | 324 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
[3e8cc8b] | 325 |
|
---|
| 326 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
| 327 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
| 328 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
| 329 |
|
---|
| 330 | UBO_VP_mats asteroid_VP_mats;
|
---|
| 331 |
|
---|
[237cbec] | 332 | vector<SceneObject<LaserVertex, SSBO_Laser>> laserObjects;
|
---|
| 333 |
|
---|
| 334 | vector<VkBuffer> uniformBuffers_laserPipeline;
|
---|
| 335 | vector<VkDeviceMemory> uniformBuffersMemory_laserPipeline;
|
---|
| 336 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_laserPipeline;
|
---|
| 337 |
|
---|
| 338 | UBO_VP_mats laser_VP_mats;
|
---|
| 339 |
|
---|
[4a9416a] | 340 | vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
|
---|
| 341 |
|
---|
| 342 | vector<VkBuffer> uniformBuffers_explosionPipeline;
|
---|
| 343 | vector<VkDeviceMemory> uniformBuffersMemory_explosionPipeline;
|
---|
| 344 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_explosionPipeline;
|
---|
| 345 |
|
---|
| 346 | UBO_Explosion explosion_UBO;
|
---|
| 347 |
|
---|
[7297892] | 348 | vector<BaseEffectOverTime*> effects;
|
---|
| 349 |
|
---|
[0807aeb] | 350 | float shipSpeed = 0.5f;
|
---|
| 351 | float asteroidSpeed = 2.0f;
|
---|
| 352 |
|
---|
| 353 | float spawnRate_asteroid = 0.5;
|
---|
| 354 | float lastSpawn_asteroid;
|
---|
[4ece3bf] | 355 |
|
---|
[1f81ecc] | 356 | unsigned int leftLaserIdx = -1;
|
---|
[7297892] | 357 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
---|
[1f81ecc] | 358 |
|
---|
| 359 | unsigned int rightLaserIdx = -1;
|
---|
[7297892] | 360 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
---|
[1f81ecc] | 361 |
|
---|
[20e4c2b] | 362 | /*** High-level vars ***/
|
---|
| 363 |
|
---|
[301c90a] | 364 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
| 365 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
[20e4c2b] | 366 |
|
---|
| 367 | map<string, vector<UIValue>> valueLists;
|
---|
| 368 |
|
---|
| 369 | int score;
|
---|
| 370 | float fps;
|
---|
| 371 |
|
---|
| 372 | // TODO: Make a separate TImer class
|
---|
| 373 | // It could also deal with the steady_clock vs high_resolution_clock issue
|
---|
| 374 | time_point<steady_clock> startTime;
|
---|
| 375 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
| 376 |
|
---|
| 377 | int frameCount;
|
---|
| 378 |
|
---|
| 379 | /*** Functions ***/
|
---|
| 380 |
|
---|
[4e705d6] | 381 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 382 | void initVulkan();
|
---|
[f97c5e7] | 383 | void initGraphicsPipelines();
|
---|
[15104a8] | 384 | void initMatrices();
|
---|
[20e4c2b] | 385 | void renderLoop();
|
---|
[3f32dfd] | 386 | void updateScene();
|
---|
[0df3c9a] | 387 | void cleanup();
|
---|
[c1d9b2a] | 388 |
|
---|
[c324d6a] | 389 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
[c1d9b2a] | 390 | void setupDebugMessenger();
|
---|
| 391 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
[90a424f] | 392 | void createVulkanSurface();
|
---|
[fe5c3ba] | 393 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[fa9fa1c] | 394 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
[c324d6a] | 395 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
[c1c2021] | 396 | const vector<const char*>& deviceExtensions);
|
---|
[3f32dfd] | 397 | void chooseSwapChainProperties();
|
---|
[502bd0b] | 398 | void createSwapChain();
|
---|
[f94eea9] | 399 | void createImageViews();
|
---|
[6fc24c7] | 400 | void createRenderPass();
|
---|
[3f32dfd] | 401 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
| 402 | void createResourceCommandPool();
|
---|
[9c0a614] | 403 | void createCommandPools();
|
---|
[603b5bc] | 404 | void createImageResources();
|
---|
| 405 | void createFramebuffers();
|
---|
| 406 | void createCommandBuffers();
|
---|
[34bdf3a] | 407 | void createSyncObjects();
|
---|
[f94eea9] | 408 |
|
---|
[3f32dfd] | 409 | void createTextureSampler();
|
---|
| 410 |
|
---|
[3b7d497] | 411 | void createImguiDescriptorPool();
|
---|
| 412 | void destroyImguiDescriptorPool();
|
---|
| 413 |
|
---|
[4994692] | 414 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
| 415 | // stop using objects.back() to access the object that was just created
|
---|
[2d87297] | 416 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 417 | SceneObject<VertexType, SSBOType>& addObject(
|
---|
| 418 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 419 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 420 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 421 | bool pipelinesCreated);
|
---|
[0fe8433] | 422 |
|
---|
[2da64ef] | 423 | template<class VertexType, class SSBOType>
|
---|
| 424 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[4994692] | 425 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
[2da64ef] | 426 |
|
---|
[1f81ecc] | 427 | template<class VertexType, class SSBOType>
|
---|
| 428 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 429 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
---|
| 430 |
|
---|
[06d959f] | 431 | template<class VertexType>
|
---|
| 432 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 433 |
|
---|
[cf727ca] | 434 | template<class VertexType>
|
---|
| 435 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 436 |
|
---|
[3b84bb6] | 437 | template<class VertexType, class SSBOType>
|
---|
| 438 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
[a79be34] | 439 |
|
---|
[52a02e6] | 440 | void addLaser(vec3 start, vec3 end, vec3 color, float width);
|
---|
| 441 | void translateLaser(size_t index, const vec3& translation);
|
---|
| 442 | void updateLaserTarget(size_t index);
|
---|
| 443 | bool getLaserAndAsteroidIntersection(SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid,
|
---|
| 444 | vec3& start, vec3& end, vec3& intersection);
|
---|
| 445 |
|
---|
[4a9416a] | 446 | void addExplosion(mat4 model_mat, float duration, float cur_time);
|
---|
| 447 |
|
---|
[055750a] | 448 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
[4994692] | 449 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
| 450 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
[f97c5e7] | 451 |
|
---|
[ea2b4dc] | 452 | void renderFrame(ImDrawData* draw_data);
|
---|
[4e2c709] | 453 | void presentFrame();
|
---|
| 454 |
|
---|
[d2d9286] | 455 | void recreateSwapChain();
|
---|
| 456 |
|
---|
[c1c2021] | 457 | void cleanupSwapChain();
|
---|
[20e4c2b] | 458 |
|
---|
| 459 | /*** High-level functions ***/
|
---|
| 460 |
|
---|
[301c90a] | 461 | void renderMainScreen(int width, int height);
|
---|
| 462 | void renderGameScreen(int width, int height);
|
---|
[20e4c2b] | 463 |
|
---|
| 464 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
| 465 | void renderGuiValueList(vector<UIValue>& values);
|
---|
| 466 |
|
---|
[301c90a] | 467 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
[20e4c2b] | 468 | void quitGame();
|
---|
[e8ebc76] | 469 | };
|
---|
| 470 |
|
---|
[4a9416a] | 471 | // Start of specialized no-op functions
|
---|
| 472 |
|
---|
| 473 | template<>
|
---|
| 474 | inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | // End of specialized no-op functions
|
---|
| 478 |
|
---|
[3b84bb6] | 479 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
| 480 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
| 481 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
---|
[2ba5617] | 482 |
|
---|
| 483 | // TODO: Maybe return a reference to the object from this method if I decide that updating it
|
---|
| 484 | // immediately after creation is a good idea (such as setting model_base)
|
---|
| 485 | // Currently, model_base is set like this in a few places and the radius is set for asteroids
|
---|
| 486 | // to account for scaling
|
---|
[2d87297] | 487 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 488 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
| 489 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[2d87297] | 490 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
[3b84bb6] | 491 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 492 | bool pipelinesCreated) {
|
---|
[2ba5617] | 493 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
| 494 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
[0fe8433] | 495 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 496 |
|
---|
| 497 | for (uint16_t& idx : indices) {
|
---|
| 498 | idx += numVertices;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[5ba732a] | 501 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
---|
[3b84bb6] | 502 |
|
---|
[2ba5617] | 503 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
[1f81ecc] | 504 |
|
---|
[4a9416a] | 505 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
[1f81ecc] | 506 | centerObject(obj);
|
---|
| 507 | }
|
---|
[2ba5617] | 508 |
|
---|
[4994692] | 509 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
[9067efc] | 510 | resourceCommandPool, graphicsQueue);
|
---|
[0fe8433] | 511 |
|
---|
[3b84bb6] | 512 | if (pipelinesCreated) {
|
---|
[44f23af] | 513 | vkDeviceWaitIdle(device);
|
---|
[9c0a614] | 514 |
|
---|
| 515 | for (uint32_t i = 0; i < swapChainImageCount; i++) {
|
---|
| 516 | vkFreeCommandBuffers(device, commandPools[i], 1, &commandBuffers[i]);
|
---|
| 517 | }
|
---|
[44f23af] | 518 |
|
---|
| 519 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
---|
| 520 | // one SSBO is resized.
|
---|
| 521 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
---|
| 522 | // are created and then recreate each of the corresponding pipelines only once per frame
|
---|
[3b84bb6] | 523 | if (storageBufferResized) {
|
---|
[44f23af] | 524 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
| 525 | pipeline.createDescriptorPool(swapChainImages);
|
---|
| 526 | pipeline.createDescriptorSets(swapChainImages);
|
---|
[3b84bb6] | 527 | }
|
---|
| 528 |
|
---|
| 529 | createCommandBuffers();
|
---|
| 530 | }
|
---|
[4994692] | 531 |
|
---|
| 532 | return obj;
|
---|
[0fe8433] | 533 | }
|
---|
| 534 |
|
---|
[0807aeb] | 535 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
[2da64ef] | 536 | template<class VertexType, class SSBOType>
|
---|
| 537 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 538 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
---|
| 539 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
---|
| 540 |
|
---|
| 541 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
[2ba5617] | 542 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[2da64ef] | 543 |
|
---|
| 544 | pipeline.updateObject(index, obj.ssbo);
|
---|
[5ba732a] | 545 |
|
---|
| 546 | obj.modified = false;
|
---|
[2da64ef] | 547 | }
|
---|
| 548 |
|
---|
[1f81ecc] | 549 | template<class VertexType, class SSBOType>
|
---|
| 550 | void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 551 | SceneObject<VertexType, SSBOType>& obj, size_t index) {
|
---|
[9067efc] | 552 | pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
|
---|
[1f81ecc] | 553 | }
|
---|
| 554 |
|
---|
[06d959f] | 555 | template<class VertexType>
|
---|
| 556 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 557 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 558 | vec3 p1 = vertices[i].pos;
|
---|
| 559 | vec3 p2 = vertices[i+1].pos;
|
---|
| 560 | vec3 p3 = vertices[i+2].pos;
|
---|
| 561 |
|
---|
[a79be34] | 562 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
[06d959f] | 563 |
|
---|
| 564 | // Add the same normal for all 3 vertices
|
---|
| 565 | vertices[i].normal = normal;
|
---|
| 566 | vertices[i+1].normal = normal;
|
---|
| 567 | vertices[i+2].normal = normal;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | return vertices;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[cf727ca] | 573 | template<class VertexType>
|
---|
| 574 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 575 | for (VertexType& vertex : vertices) {
|
---|
| 576 | vertex.objIndex = objIndex;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | return vertices;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[3b84bb6] | 582 | template<class VertexType, class SSBOType>
|
---|
| 583 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
| 584 | vector<VertexType>& vertices = object.vertices;
|
---|
| 585 |
|
---|
[a79be34] | 586 | float min_x = vertices[0].pos.x;
|
---|
| 587 | float max_x = vertices[0].pos.x;
|
---|
| 588 | float min_y = vertices[0].pos.y;
|
---|
| 589 | float max_y = vertices[0].pos.y;
|
---|
| 590 | float min_z = vertices[0].pos.z;
|
---|
| 591 | float max_z = vertices[0].pos.z;
|
---|
| 592 |
|
---|
| 593 | // start from the second point
|
---|
| 594 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
[3b84bb6] | 595 | vec3& pos = vertices[i].pos;
|
---|
| 596 |
|
---|
| 597 | if (min_x > pos.x) {
|
---|
| 598 | min_x = pos.x;
|
---|
| 599 | } else if (max_x < pos.x) {
|
---|
| 600 | max_x = pos.x;
|
---|
[a79be34] | 601 | }
|
---|
| 602 |
|
---|
[3b84bb6] | 603 | if (min_y > pos.y) {
|
---|
| 604 | min_y = pos.y;
|
---|
| 605 | } else if (max_y < pos.y) {
|
---|
| 606 | max_y = pos.y;
|
---|
[a79be34] | 607 | }
|
---|
| 608 |
|
---|
[3b84bb6] | 609 | if (min_z > pos.z) {
|
---|
| 610 | min_z = pos.z;
|
---|
| 611 | } else if (max_z < pos.z) {
|
---|
| 612 | max_z = pos.z;
|
---|
[a79be34] | 613 | }
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 617 |
|
---|
| 618 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 619 | vertices[i].pos -= center;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[2ba5617] | 622 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
| 623 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
| 624 |
|
---|
[3b84bb6] | 625 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
[a79be34] | 626 | }
|
---|
| 627 |
|
---|
[3b84bb6] | 628 | #endif // _VULKAN_GAME_H
|
---|