| 1 | #ifndef _VULKAN_GAME_H
|
|---|
| 2 | #define _VULKAN_GAME_H
|
|---|
| 3 |
|
|---|
| 4 | #define GLM_FORCE_RADIANS
|
|---|
| 5 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
|---|
| 6 | #define GLM_FORCE_RIGHT_HANDED
|
|---|
| 7 |
|
|---|
| 8 | #include <glm/glm.hpp>
|
|---|
| 9 | #include <glm/gtc/matrix_transform.hpp>
|
|---|
| 10 |
|
|---|
| 11 | #include "game-gui-sdl.hpp"
|
|---|
| 12 | #include "graphics-pipeline_vulkan.hpp"
|
|---|
| 13 |
|
|---|
| 14 | #include "vulkan-utils.hpp"
|
|---|
| 15 |
|
|---|
| 16 | using namespace glm;
|
|---|
| 17 |
|
|---|
| 18 | #ifdef NDEBUG
|
|---|
| 19 | const bool ENABLE_VALIDATION_LAYERS = false;
|
|---|
| 20 | #else
|
|---|
| 21 | const bool ENABLE_VALIDATION_LAYERS = true;
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | struct OverlayVertex {
|
|---|
| 25 | vec3 pos;
|
|---|
| 26 | vec2 texCoord;
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | struct ModelVertex {
|
|---|
| 30 | vec3 pos;
|
|---|
| 31 | vec3 color;
|
|---|
| 32 | vec2 texCoord;
|
|---|
| 33 | unsigned int objIndex;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | struct ShipVertex {
|
|---|
| 37 | vec3 pos;
|
|---|
| 38 | vec3 color;
|
|---|
| 39 | vec3 normal;
|
|---|
| 40 | unsigned int objIndex;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | struct AsteroidVertex {
|
|---|
| 44 | vec3 pos;
|
|---|
| 45 | vec3 color;
|
|---|
| 46 | vec3 normal;
|
|---|
| 47 | unsigned int objIndex;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
|---|
| 51 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
|---|
| 52 | template<class VertexType, class SSBOType>
|
|---|
| 53 | struct SceneObject {
|
|---|
| 54 | vector<VertexType> vertices;
|
|---|
| 55 | vector<uint16_t> indices;
|
|---|
| 56 | SSBOType ssbo;
|
|---|
| 57 |
|
|---|
| 58 | mat4 model_base;
|
|---|
| 59 | mat4 model_transform;
|
|---|
| 60 | vec3 center;
|
|---|
| 61 | float radius;
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | struct UBO_VP_mats {
|
|---|
| 65 | alignas(16) mat4 view;
|
|---|
| 66 | alignas(16) mat4 proj;
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | struct SSBO_ModelObject {
|
|---|
| 70 | alignas(16) mat4 model;
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | struct SSBO_Asteroid {
|
|---|
| 74 | alignas(16) mat4 model;
|
|---|
| 75 | alignas(4) float hp;
|
|---|
| 76 | alignas(4) unsigned int deleted;
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | // Have to figure out how to include an optional ssbo parameter for each object
|
|---|
| 80 | // Could probably use the same approach to make indices optional
|
|---|
| 81 |
|
|---|
| 82 | class VulkanGame {
|
|---|
| 83 | public:
|
|---|
| 84 | VulkanGame(int maxFramesInFlight);
|
|---|
| 85 | ~VulkanGame();
|
|---|
| 86 |
|
|---|
| 87 | void run(int width, int height, unsigned char guiFlags);
|
|---|
| 88 |
|
|---|
| 89 | private:
|
|---|
| 90 | const int MAX_FRAMES_IN_FLIGHT;
|
|---|
| 91 |
|
|---|
| 92 | const float NEAR_CLIP = 0.1f;
|
|---|
| 93 | const float FAR_CLIP = 100.0f;
|
|---|
| 94 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
|---|
| 95 |
|
|---|
| 96 | vec3 cam_pos;
|
|---|
| 97 |
|
|---|
| 98 | GameGui* gui;
|
|---|
| 99 |
|
|---|
| 100 | SDL_version sdlVersion;
|
|---|
| 101 | SDL_Window* window = nullptr;
|
|---|
| 102 | SDL_Renderer* renderer = nullptr;
|
|---|
| 103 |
|
|---|
| 104 | SDL_Texture* uiOverlay = nullptr;
|
|---|
| 105 |
|
|---|
| 106 | VkInstance instance;
|
|---|
| 107 | VkDebugUtilsMessengerEXT debugMessenger;
|
|---|
| 108 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
|---|
| 109 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|---|
| 110 | VkDevice device;
|
|---|
| 111 |
|
|---|
| 112 | VkQueue graphicsQueue;
|
|---|
| 113 | VkQueue presentQueue;
|
|---|
| 114 |
|
|---|
| 115 | VkSwapchainKHR swapChain;
|
|---|
| 116 | vector<VkImage> swapChainImages;
|
|---|
| 117 | VkFormat swapChainImageFormat;
|
|---|
| 118 | VkExtent2D swapChainExtent;
|
|---|
| 119 | vector<VkImageView> swapChainImageViews;
|
|---|
| 120 | vector<VkFramebuffer> swapChainFramebuffers;
|
|---|
| 121 |
|
|---|
| 122 | VkRenderPass renderPass;
|
|---|
| 123 | VkCommandPool commandPool;
|
|---|
| 124 | vector<VkCommandBuffer> commandBuffers;
|
|---|
| 125 |
|
|---|
| 126 | VulkanImage depthImage;
|
|---|
| 127 |
|
|---|
| 128 | VkSampler textureSampler;
|
|---|
| 129 |
|
|---|
| 130 | VulkanImage floorTextureImage;
|
|---|
| 131 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
|---|
| 132 |
|
|---|
| 133 | VulkanImage sdlOverlayImage;
|
|---|
| 134 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
|---|
| 135 |
|
|---|
| 136 | TTF_Font* font;
|
|---|
| 137 | SDL_Texture* fontSDLTexture;
|
|---|
| 138 |
|
|---|
| 139 | SDL_Texture* imageSDLTexture;
|
|---|
| 140 |
|
|---|
| 141 | vector<VkSemaphore> imageAvailableSemaphores;
|
|---|
| 142 | vector<VkSemaphore> renderFinishedSemaphores;
|
|---|
| 143 | vector<VkFence> inFlightFences;
|
|---|
| 144 |
|
|---|
| 145 | size_t currentFrame;
|
|---|
| 146 |
|
|---|
| 147 | bool framebufferResized;
|
|---|
| 148 |
|
|---|
| 149 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
|---|
| 150 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
|---|
| 151 |
|
|---|
| 152 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
|---|
| 153 | // per pipeline.
|
|---|
| 154 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
|---|
| 155 | // the objects vector, the ubo, and the ssbo
|
|---|
| 156 |
|
|---|
| 157 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
|---|
| 158 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
|---|
| 159 |
|
|---|
| 160 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
|---|
| 161 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
|---|
| 162 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
|---|
| 163 |
|
|---|
| 164 | UBO_VP_mats object_VP_mats;
|
|---|
| 165 |
|
|---|
| 166 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
|---|
| 167 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
|---|
| 168 |
|
|---|
| 169 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
|---|
| 170 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
|---|
| 171 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
|---|
| 172 |
|
|---|
| 173 | UBO_VP_mats ship_VP_mats;
|
|---|
| 174 |
|
|---|
| 175 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
|---|
| 176 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
|---|
| 177 |
|
|---|
| 178 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
|---|
| 179 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
|---|
| 180 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
|---|
| 181 |
|
|---|
| 182 | UBO_VP_mats asteroid_VP_mats;
|
|---|
| 183 |
|
|---|
| 184 | Uint64 curTime, prevTime;
|
|---|
| 185 | double elapsedTime;
|
|---|
| 186 |
|
|---|
| 187 | bool initWindow(int width, int height, unsigned char guiFlags);
|
|---|
| 188 | void initVulkan();
|
|---|
| 189 | void initGraphicsPipelines();
|
|---|
| 190 | void initMatrices();
|
|---|
| 191 | void mainLoop();
|
|---|
| 192 | void updateScene(uint32_t currentImage);
|
|---|
| 193 | void renderUI();
|
|---|
| 194 | void renderScene();
|
|---|
| 195 | void cleanup();
|
|---|
| 196 |
|
|---|
| 197 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
|---|
| 198 | void setupDebugMessenger();
|
|---|
| 199 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
|---|
| 200 | void createVulkanSurface();
|
|---|
| 201 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
|---|
| 202 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
|---|
| 203 | void createLogicalDevice(
|
|---|
| 204 | const vector<const char*> validationLayers,
|
|---|
| 205 | const vector<const char*>& deviceExtensions);
|
|---|
| 206 | void createSwapChain();
|
|---|
| 207 | void createImageViews();
|
|---|
| 208 | void createRenderPass();
|
|---|
| 209 | VkFormat findDepthFormat();
|
|---|
| 210 | void createCommandPool();
|
|---|
| 211 | void createImageResources();
|
|---|
| 212 |
|
|---|
| 213 | void createTextureSampler();
|
|---|
| 214 | void createFramebuffers();
|
|---|
| 215 | void createCommandBuffers();
|
|---|
| 216 | void createSyncObjects();
|
|---|
| 217 |
|
|---|
| 218 | template<class VertexType, class SSBOType>
|
|---|
| 219 | void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 220 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
|---|
| 221 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
|---|
| 222 | bool pipelinesCreated);
|
|---|
| 223 |
|
|---|
| 224 | template<class VertexType, class SSBOType>
|
|---|
| 225 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 226 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
|---|
| 227 |
|
|---|
| 228 | template<class VertexType>
|
|---|
| 229 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
|---|
| 230 |
|
|---|
| 231 | template<class VertexType>
|
|---|
| 232 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
|---|
| 233 |
|
|---|
| 234 | template<class VertexType, class SSBOType>
|
|---|
| 235 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
|---|
| 236 |
|
|---|
| 237 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
|---|
| 238 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
|
|---|
| 239 |
|
|---|
| 240 | void recreateSwapChain();
|
|---|
| 241 |
|
|---|
| 242 | void cleanupSwapChain();
|
|---|
| 243 |
|
|---|
| 244 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
|---|
| 245 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|---|
| 246 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|---|
| 247 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|---|
| 248 | void* pUserData);
|
|---|
| 249 | };
|
|---|
| 250 |
|
|---|
| 251 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
|---|
| 252 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
|---|
| 253 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
|---|
| 254 | template<class VertexType, class SSBOType>
|
|---|
| 255 | void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 256 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
|---|
| 257 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
|---|
| 258 | bool pipelinesCreated) {
|
|---|
| 259 | size_t numVertices = pipeline.getNumVertices();
|
|---|
| 260 |
|
|---|
| 261 | for (uint16_t& idx : indices) {
|
|---|
| 262 | idx += numVertices;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
|
|---|
| 266 | centerObject(objects.back());
|
|---|
| 267 |
|
|---|
| 268 | bool storageBufferResized = pipeline.addObject(vertices, indices, ssbo, commandPool, graphicsQueue);
|
|---|
| 269 |
|
|---|
| 270 | if (pipelinesCreated) {
|
|---|
| 271 | vkDeviceWaitIdle(device);
|
|---|
| 272 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
|---|
| 273 |
|
|---|
| 274 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
|---|
| 275 | // one SSBO is resized.
|
|---|
| 276 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
|---|
| 277 | // are created and then recreate each of the corresponding pipelines only once per frame
|
|---|
| 278 | if (storageBufferResized) {
|
|---|
| 279 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
|---|
| 280 | pipeline.createDescriptorPool(swapChainImages);
|
|---|
| 281 | pipeline.createDescriptorSets(swapChainImages);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | createCommandBuffers();
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | template<class VertexType, class SSBOType>
|
|---|
| 289 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
|---|
| 290 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
|---|
| 291 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
|---|
| 292 |
|
|---|
| 293 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
|---|
| 294 |
|
|---|
| 295 | // could probably re-calculate the object center here based on model
|
|---|
| 296 | // I think the center should be calculated by using model * vec3(0, 0, 0)
|
|---|
| 297 | // model_base is currently only used to set the original location of the ship and asteroids
|
|---|
| 298 |
|
|---|
| 299 | pipeline.updateObject(index, obj.ssbo);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | template<class VertexType>
|
|---|
| 303 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
|---|
| 304 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
|---|
| 305 | vec3 p1 = vertices[i].pos;
|
|---|
| 306 | vec3 p2 = vertices[i+1].pos;
|
|---|
| 307 | vec3 p3 = vertices[i+2].pos;
|
|---|
| 308 |
|
|---|
| 309 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
|---|
| 310 |
|
|---|
| 311 | // Add the same normal for all 3 vertices
|
|---|
| 312 | vertices[i].normal = normal;
|
|---|
| 313 | vertices[i+1].normal = normal;
|
|---|
| 314 | vertices[i+2].normal = normal;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | return vertices;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | template<class VertexType>
|
|---|
| 321 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
|---|
| 322 | for (VertexType& vertex : vertices) {
|
|---|
| 323 | vertex.objIndex = objIndex;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | return vertices;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | template<class VertexType, class SSBOType>
|
|---|
| 330 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
|---|
| 331 | vector<VertexType>& vertices = object.vertices;
|
|---|
| 332 |
|
|---|
| 333 | float min_x = vertices[0].pos.x;
|
|---|
| 334 | float max_x = vertices[0].pos.x;
|
|---|
| 335 | float min_y = vertices[0].pos.y;
|
|---|
| 336 | float max_y = vertices[0].pos.y;
|
|---|
| 337 | float min_z = vertices[0].pos.z;
|
|---|
| 338 | float max_z = vertices[0].pos.z;
|
|---|
| 339 |
|
|---|
| 340 | // start from the second point
|
|---|
| 341 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
|---|
| 342 | vec3& pos = vertices[i].pos;
|
|---|
| 343 |
|
|---|
| 344 | if (min_x > pos.x) {
|
|---|
| 345 | min_x = pos.x;
|
|---|
| 346 | } else if (max_x < pos.x) {
|
|---|
| 347 | max_x = pos.x;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | if (min_y > pos.y) {
|
|---|
| 351 | min_y = pos.y;
|
|---|
| 352 | } else if (max_y < pos.y) {
|
|---|
| 353 | max_y = pos.y;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | if (min_z > pos.z) {
|
|---|
| 357 | min_z = pos.z;
|
|---|
| 358 | } else if (max_z < pos.z) {
|
|---|
| 359 | max_z = pos.z;
|
|---|
| 360 | }
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
|---|
| 364 |
|
|---|
| 365 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
|---|
| 366 | vertices[i].pos -= center;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | object.radius = std::max(center.x, center.y);
|
|---|
| 370 | object.radius = std::max(object.radius, center.z);
|
|---|
| 371 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | #endif // _VULKAN_GAME_H
|
|---|