| 1 | #ifndef _VULKAN_GAME_H
|
|---|
| 2 | #define _VULKAN_GAME_H
|
|---|
| 3 |
|
|---|
| 4 | #include <algorithm>
|
|---|
| 5 | #include <chrono>
|
|---|
| 6 | #include <map>
|
|---|
| 7 | #include <vector>
|
|---|
| 8 |
|
|---|
| 9 | #include <vulkan/vulkan.h>
|
|---|
| 10 |
|
|---|
| 11 | #include <SDL2/SDL.h>
|
|---|
| 12 | #include <SDL2/SDL_ttf.h>
|
|---|
| 13 |
|
|---|
| 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
|
|---|
| 16 | #define GLM_FORCE_RIGHT_HANDED
|
|---|
| 17 |
|
|---|
| 18 | #include <glm/glm.hpp>
|
|---|
| 19 | #include <glm/gtc/matrix_transform.hpp>
|
|---|
| 20 |
|
|---|
| 21 | #include "IMGUI/imgui_impl_vulkan.h"
|
|---|
| 22 |
|
|---|
| 23 | #include "consts.hpp"
|
|---|
| 24 | #include "graphics-pipeline_vulkan.hpp"
|
|---|
| 25 | #include "game-gui-sdl.hpp"
|
|---|
| 26 | #include "utils.hpp"
|
|---|
| 27 | #include "vulkan-utils.hpp"
|
|---|
| 28 |
|
|---|
| 29 | using namespace glm;
|
|---|
| 30 | using namespace std::chrono;
|
|---|
| 31 |
|
|---|
| 32 | #ifdef NDEBUG
|
|---|
| 33 | const bool ENABLE_VALIDATION_LAYERS = false;
|
|---|
| 34 | #else
|
|---|
| 35 | const bool ENABLE_VALIDATION_LAYERS = true;
|
|---|
| 36 | #endif
|
|---|
| 37 |
|
|---|
| 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 |
|
|---|
| 41 | struct OverlayVertex {
|
|---|
| 42 | vec3 pos;
|
|---|
| 43 | vec2 texCoord;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | struct ModelVertex {
|
|---|
| 47 | vec3 pos;
|
|---|
| 48 | vec3 color;
|
|---|
| 49 | vec2 texCoord;
|
|---|
| 50 | vec3 normal;
|
|---|
| 51 | unsigned int objIndex;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | struct LaserVertex {
|
|---|
| 55 | vec3 pos;
|
|---|
| 56 | vec2 texCoord;
|
|---|
| 57 | unsigned int objIndex;
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | struct ExplosionVertex {
|
|---|
| 61 | vec3 particleStartVelocity;
|
|---|
| 62 | float particleStartTime;
|
|---|
| 63 | unsigned int objIndex;
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | struct SSBO_ModelObject {
|
|---|
| 67 | alignas(16) mat4 model;
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | struct SSBO_Asteroid {
|
|---|
| 71 | alignas(16) mat4 model;
|
|---|
| 72 | alignas(4) float hp;
|
|---|
| 73 | alignas(4) unsigned int deleted;
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | struct SSBO_Laser {
|
|---|
| 77 | alignas(16) mat4 model;
|
|---|
| 78 | alignas(4) vec3 color;
|
|---|
| 79 | alignas(4) unsigned int deleted;
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 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 |
|
|---|
| 89 | struct UBO_VP_mats {
|
|---|
| 90 | alignas(16) mat4 view;
|
|---|
| 91 | alignas(16) mat4 proj;
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | struct UBO_Explosion {
|
|---|
| 95 | alignas(16) mat4 view;
|
|---|
| 96 | alignas(16) mat4 proj;
|
|---|
| 97 | alignas(4) float cur_time;
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 121 | bool modified;
|
|---|
| 122 |
|
|---|
| 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
|
|---|
| 127 | SceneObject<ModelVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
|---|
| 128 | };
|
|---|
| 129 |
|
|---|
| 130 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
|---|
| 131 | // Could probably use the same approach to make indices optional
|
|---|
| 132 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
|---|
| 133 | // them mamdatory
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
|---|
| 137 |
|
|---|
| 138 | struct BaseEffectOverTime {
|
|---|
| 139 | bool deleted;
|
|---|
| 140 |
|
|---|
| 141 | virtual void applyEffect(float curTime) = 0;
|
|---|
| 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>& 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>& 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) {
|
|---|
| 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 |
|
|---|
| 177 | void applyEffect(float curTime) {
|
|---|
| 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 |
|
|---|
| 194 | // TODO: Maybe move this to a different header
|
|---|
| 195 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 215 | class VulkanGame {
|
|---|
| 216 |
|
|---|
| 217 | public:
|
|---|
| 218 |
|
|---|
| 219 | VulkanGame();
|
|---|
| 220 | ~VulkanGame();
|
|---|
| 221 |
|
|---|
| 222 | void run(int width, int height, unsigned char guiFlags);
|
|---|
| 223 |
|
|---|
| 224 | private:
|
|---|
| 225 |
|
|---|
| 226 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
|---|
| 227 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|---|
| 228 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|---|
| 229 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|---|
| 230 | void* pUserData);
|
|---|
| 231 |
|
|---|
| 232 | // TODO: Maybe pass these in as parameters to some Camera class
|
|---|
| 233 | const float NEAR_CLIP = 0.1f;
|
|---|
| 234 | const float FAR_CLIP = 100.0f;
|
|---|
| 235 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 deg
|
|---|
| 236 |
|
|---|
| 237 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
|---|
| 238 | const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
|
|---|
| 239 |
|
|---|
| 240 | bool done;
|
|---|
| 241 |
|
|---|
| 242 | vec3 cam_pos;
|
|---|
| 243 |
|
|---|
| 244 | // TODO: Good place to start using smart pointers
|
|---|
| 245 | GameGui* gui;
|
|---|
| 246 |
|
|---|
| 247 | SDL_version sdlVersion;
|
|---|
| 248 | SDL_Window* window = nullptr;
|
|---|
| 249 |
|
|---|
| 250 | int drawableWidth, drawableHeight;
|
|---|
| 251 |
|
|---|
| 252 | VkInstance instance;
|
|---|
| 253 | VkDebugUtilsMessengerEXT debugMessenger;
|
|---|
| 254 | VkSurfaceKHR vulkanSurface;
|
|---|
| 255 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|---|
| 256 | VkDevice device;
|
|---|
| 257 |
|
|---|
| 258 | VkQueue graphicsQueue;
|
|---|
| 259 | VkQueue presentQueue;
|
|---|
| 260 |
|
|---|
| 261 | // TODO: Maybe make a swapchain struct for convenience
|
|---|
| 262 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
|---|
| 263 | VkPresentModeKHR swapChainPresentMode;
|
|---|
| 264 | VkExtent2D swapChainExtent;
|
|---|
| 265 | uint32_t swapChainMinImageCount;
|
|---|
| 266 | uint32_t swapChainImageCount;
|
|---|
| 267 | VkSwapchainKHR swapChain;
|
|---|
| 268 | vector<VkImage> swapChainImages;
|
|---|
| 269 | vector<VkImageView> swapChainImageViews;
|
|---|
| 270 | vector<VkFramebuffer> swapChainFramebuffers;
|
|---|
| 271 |
|
|---|
| 272 | VkRenderPass renderPass;
|
|---|
| 273 |
|
|---|
| 274 | VkCommandPool resourceCommandPool;
|
|---|
| 275 |
|
|---|
| 276 | vector<VkCommandPool> commandPools;
|
|---|
| 277 | vector<VkCommandBuffer> commandBuffers;
|
|---|
| 278 |
|
|---|
| 279 | VulkanImage depthImage;
|
|---|
| 280 |
|
|---|
| 281 | // These are per frame
|
|---|
| 282 | vector<VkSemaphore> imageAcquiredSemaphores;
|
|---|
| 283 | vector<VkSemaphore> renderCompleteSemaphores;
|
|---|
| 284 |
|
|---|
| 285 | // These are per swap chain image
|
|---|
| 286 | vector<VkFence> inFlightFences;
|
|---|
| 287 |
|
|---|
| 288 | uint32_t imageIndex;
|
|---|
| 289 | uint32_t currentFrame;
|
|---|
| 290 |
|
|---|
| 291 | bool shouldRecreateSwapChain;
|
|---|
| 292 |
|
|---|
| 293 | VkSampler textureSampler;
|
|---|
| 294 |
|
|---|
| 295 | VulkanImage floorTextureImage;
|
|---|
| 296 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
|---|
| 297 |
|
|---|
| 298 | VulkanImage laserTextureImage;
|
|---|
| 299 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
|---|
| 300 |
|
|---|
| 301 | mat4 viewMat, projMat;
|
|---|
| 302 |
|
|---|
| 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
|
|---|
| 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;
|
|---|
| 316 |
|
|---|
| 317 | StorageBufferSet storageBuffers_modelPipeline;
|
|---|
| 318 | StorageBufferSet storageBuffers_shipPipeline;
|
|---|
| 319 | StorageBufferSet storageBuffers_asteroidPipeline;
|
|---|
| 320 | StorageBufferSet storageBuffers_laserPipeline;
|
|---|
| 321 | StorageBufferSet storageBuffers_explosionPipeline;
|
|---|
| 322 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 331 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
|---|
| 332 |
|
|---|
| 333 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
|---|
| 334 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
|---|
| 335 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
|---|
| 336 |
|
|---|
| 337 | UBO_VP_mats object_VP_mats;
|
|---|
| 338 |
|
|---|
| 339 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> shipObjects;
|
|---|
| 340 |
|
|---|
| 341 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
|---|
| 342 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
|---|
| 343 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
|---|
| 344 |
|
|---|
| 345 | UBO_VP_mats ship_VP_mats;
|
|---|
| 346 |
|
|---|
| 347 | vector<SceneObject<ModelVertex, SSBO_Asteroid>> asteroidObjects;
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 371 | vector<BaseEffectOverTime*> effects;
|
|---|
| 372 |
|
|---|
| 373 | float shipSpeed = 0.5f;
|
|---|
| 374 | float asteroidSpeed = 2.0f;
|
|---|
| 375 |
|
|---|
| 376 | float spawnRate_asteroid = 0.5;
|
|---|
| 377 | float lastSpawn_asteroid;
|
|---|
| 378 |
|
|---|
| 379 | unsigned int leftLaserIdx = -1;
|
|---|
| 380 | EffectOverTime<ModelVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
|---|
| 381 |
|
|---|
| 382 | unsigned int rightLaserIdx = -1;
|
|---|
| 383 | EffectOverTime<ModelVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
|---|
| 384 |
|
|---|
| 385 | /*** High-level vars ***/
|
|---|
| 386 |
|
|---|
| 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);
|
|---|
| 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 |
|
|---|
| 403 | bool initUI(int width, int height, unsigned char guiFlags);
|
|---|
| 404 | void initVulkan();
|
|---|
| 405 | void initGraphicsPipelines();
|
|---|
| 406 | void initMatrices();
|
|---|
| 407 | void renderLoop();
|
|---|
| 408 | void updateScene();
|
|---|
| 409 | void cleanup();
|
|---|
| 410 |
|
|---|
| 411 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
|---|
| 412 | void setupDebugMessenger();
|
|---|
| 413 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
|---|
| 414 | void createVulkanSurface();
|
|---|
| 415 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
|---|
| 416 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
|---|
| 417 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
|---|
| 418 | const vector<const char*>& deviceExtensions);
|
|---|
| 419 | void chooseSwapChainProperties();
|
|---|
| 420 | void createSwapChain();
|
|---|
| 421 | void createImageViews();
|
|---|
| 422 | void createResourceCommandPool();
|
|---|
| 423 | void createImageResources();
|
|---|
| 424 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
|---|
| 425 | void createRenderPass();
|
|---|
| 426 | void createCommandPools();
|
|---|
| 427 | void createFramebuffers();
|
|---|
| 428 | void createCommandBuffers();
|
|---|
| 429 | void createSyncObjects();
|
|---|
| 430 |
|
|---|
| 431 | void createTextureSampler();
|
|---|
| 432 |
|
|---|
| 433 | void initImGuiOverlay();
|
|---|
| 434 | void cleanupImGuiOverlay();
|
|---|
| 435 |
|
|---|
| 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);
|
|---|
| 450 |
|
|---|
| 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
|
|---|
| 453 | template<class VertexType, class SSBOType>
|
|---|
| 454 | SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 455 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
|---|
| 456 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
|---|
| 457 | SSBOType ssbo, StorageBufferSet& storageBuffers,
|
|---|
| 458 | bool pipelinesCreated);
|
|---|
| 459 |
|
|---|
| 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 |
|
|---|
| 469 | template<class VertexType, class SSBOType>
|
|---|
| 470 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 471 | GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index);
|
|---|
| 472 |
|
|---|
| 473 | template<class VertexType, class SSBOType>
|
|---|
| 474 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
|---|
| 475 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
|---|
| 476 |
|
|---|
| 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);
|
|---|
| 480 | bool getLaserAndAsteroidIntersection(SceneObject<ModelVertex, SSBO_Asteroid>& asteroid,
|
|---|
| 481 | vec3& start, vec3& end, vec3& intersection);
|
|---|
| 482 |
|
|---|
| 483 | void addExplosion(mat4 model_mat, float duration, float cur_time);
|
|---|
| 484 |
|
|---|
| 485 | void renderFrame(ImDrawData* draw_data);
|
|---|
| 486 | void presentFrame();
|
|---|
| 487 |
|
|---|
| 488 | void recreateSwapChain();
|
|---|
| 489 |
|
|---|
| 490 | void cleanupSwapChain();
|
|---|
| 491 |
|
|---|
| 492 | /*** High-level functions ***/
|
|---|
| 493 |
|
|---|
| 494 | void renderMainScreen(int width, int height);
|
|---|
| 495 | void renderGameScreen(int width, int height);
|
|---|
| 496 |
|
|---|
| 497 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
|---|
| 498 | void renderGuiValueList(vector<UIValue>& values);
|
|---|
| 499 |
|
|---|
| 500 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
|---|
| 501 | void quitGame();
|
|---|
| 502 | };
|
|---|
| 503 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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()
|
|---|
| 552 | // Figure out a better way to allow the model matrix to be set during object creation
|
|---|
| 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
|
|---|
| 558 | template<class VertexType, class SSBOType>
|
|---|
| 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,
|
|---|
| 562 | SSBOType ssbo, StorageBufferSet& storageBuffers,
|
|---|
| 563 | bool pipelinesCreated) {
|
|---|
| 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
|
|---|
| 566 | size_t numVertices = pipeline.getNumVertices();
|
|---|
| 567 |
|
|---|
| 568 | for (uint16_t& idx : indices) {
|
|---|
| 569 | idx += numVertices;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
|---|
| 573 |
|
|---|
| 574 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
|---|
| 575 |
|
|---|
| 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
|
|---|
| 579 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
|---|
| 580 | centerObject(obj);
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
|
|---|
| 584 |
|
|---|
| 585 | // TODO: Probably move the resizing to the VulkanBuffer class
|
|---|
| 586 | // First, try moving this out of addObject
|
|---|
| 587 | bool resizeStorageBuffer = pipeline.numObjects == pipeline.objectCapacity;
|
|---|
| 588 |
|
|---|
| 589 | if (resizeStorageBuffer) {
|
|---|
| 590 | resizeStorageBufferSet<VertexType, SSBOType>(storageBuffers, resourceCommandPool, graphicsQueue, pipeline);
|
|---|
| 591 | pipeline.cleanup();
|
|---|
| 592 |
|
|---|
| 593 | // Assume the SSBO is always the 2nd binding
|
|---|
| 594 | // TODO: Figure out a way to make this more flexible
|
|---|
| 595 | pipeline.updateDescriptorInfo(1, &storageBuffers.infoSet);
|
|---|
| 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.
|
|---|
| 602 |
|
|---|
| 603 | if (pipelinesCreated) {
|
|---|
| 604 | // TODO: See if I can avoid doing this when recreating the pipeline
|
|---|
| 605 | vkDeviceWaitIdle(device);
|
|---|
| 606 |
|
|---|
| 607 | for (uint32_t i = 0; i < swapChainImageCount; i++) {
|
|---|
| 608 | vkFreeCommandBuffers(device, commandPools[i], 1, &commandBuffers[i]);
|
|---|
| 609 | }
|
|---|
| 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
|
|---|
| 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) {
|
|---|
| 619 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
|---|
| 620 | pipeline.createDescriptorPool(swapChainImages);
|
|---|
| 621 | pipeline.createDescriptorSets(swapChainImages);
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | createCommandBuffers();
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| 627 | return obj;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 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 | }
|
|---|
| 635 |
|
|---|
| 636 | return vertices;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 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
|
|---|
| 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;
|
|---|
| 651 | vec3 p2 = vertices[i + 1].pos;
|
|---|
| 652 | vec3 p3 = vertices[i + 2].pos;
|
|---|
| 653 |
|
|---|
| 654 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
|---|
| 655 |
|
|---|
| 656 | // Add the same normal for all 3 vertices
|
|---|
| 657 | vertices[i].normal = normal;
|
|---|
| 658 | vertices[i + 1].normal = normal;
|
|---|
| 659 | vertices[i + 2].normal = normal;
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | return vertices;
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | template<class VertexType, class SSBOType>
|
|---|
| 666 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
|---|
| 667 | vector<VertexType>& vertices = object.vertices;
|
|---|
| 668 |
|
|---|
| 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++) {
|
|---|
| 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;
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | if (min_y > pos.y) {
|
|---|
| 687 | min_y = pos.y;
|
|---|
| 688 | } else if (max_y < pos.y) {
|
|---|
| 689 | max_y = pos.y;
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | if (min_z > pos.z) {
|
|---|
| 693 | min_z = pos.z;
|
|---|
| 694 | } else if (max_z < pos.z) {
|
|---|
| 695 | max_z = pos.z;
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 708 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 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,
|
|---|
| 714 | GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index) {
|
|---|
| 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>
|
|---|
| 724 | void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
|---|
| 725 | SceneObject<VertexType, SSBOType>& obj, size_t index) {
|
|---|
| 726 | pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | #endif // _VULKAN_GAME_H
|
|---|