[99d44b2] | 1 | #ifndef _VULKAN_GAME_H
|
---|
| 2 | #define _VULKAN_GAME_H
|
---|
[e8ebc76] | 3 |
|
---|
[60578ce] | 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
|
---|
[a79be34] | 6 | #define GLM_FORCE_RIGHT_HANDED
|
---|
[60578ce] | 7 |
|
---|
[771b33a] | 8 | #include <glm/glm.hpp>
|
---|
[15104a8] | 9 | #include <glm/gtc/matrix_transform.hpp>
|
---|
[771b33a] | 10 |
|
---|
[0df3c9a] | 11 | #include "game-gui-sdl.hpp"
|
---|
[7d2b0b9] | 12 | #include "graphics-pipeline_vulkan.hpp"
|
---|
[0df3c9a] | 13 |
|
---|
[b794178] | 14 | #include "vulkan-utils.hpp"
|
---|
| 15 |
|
---|
[15104a8] | 16 | using namespace glm;
|
---|
| 17 |
|
---|
[2e77b3f] | 18 | #ifdef NDEBUG
|
---|
| 19 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
| 20 | #else
|
---|
| 21 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
[5a1ace0] | 24 | struct OverlayVertex {
|
---|
[15104a8] | 25 | vec3 pos;
|
---|
| 26 | vec2 texCoord;
|
---|
[771b33a] | 27 | };
|
---|
| 28 |
|
---|
[5a1ace0] | 29 | struct ModelVertex {
|
---|
[15104a8] | 30 | vec3 pos;
|
---|
[5a1ace0] | 31 | vec3 color;
|
---|
[15104a8] | 32 | vec2 texCoord;
|
---|
[5a1ace0] | 33 | unsigned int objIndex;
|
---|
[15104a8] | 34 | };
|
---|
| 35 |
|
---|
[3782d66] | 36 | struct ShipVertex {
|
---|
| 37 | vec3 pos;
|
---|
| 38 | vec3 color;
|
---|
[06d959f] | 39 | vec3 normal;
|
---|
[cf727ca] | 40 | unsigned int objIndex;
|
---|
[3782d66] | 41 | };
|
---|
| 42 |
|
---|
[3e8cc8b] | 43 | struct AsteroidVertex {
|
---|
| 44 | vec3 pos;
|
---|
| 45 | vec3 color;
|
---|
| 46 | vec3 normal;
|
---|
| 47 | unsigned int objIndex;
|
---|
| 48 | };
|
---|
| 49 |
|
---|
[0fe8433] | 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>
|
---|
| 53 | struct SceneObject {
|
---|
| 54 | vector<VertexType> vertices;
|
---|
| 55 | vector<uint16_t> indices;
|
---|
| 56 |
|
---|
| 57 | mat4 model_base;
|
---|
| 58 | mat4 model_transform;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
[055750a] | 61 | struct UBO_VP_mats {
|
---|
[15104a8] | 62 | alignas(16) mat4 view;
|
---|
| 63 | alignas(16) mat4 proj;
|
---|
[771b33a] | 64 | };
|
---|
| 65 |
|
---|
[055750a] | 66 | struct SBO_SceneObject {
|
---|
| 67 | alignas(16) mat4 model;
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[3e8cc8b] | 70 | struct SBO_Asteroid {
|
---|
| 71 | alignas(16) mat4 model;
|
---|
| 72 | alignas(4) float hp;
|
---|
[4ece3bf] | 73 | alignas(4) unsigned int deleted;
|
---|
[3e8cc8b] | 74 | };
|
---|
| 75 |
|
---|
[99d44b2] | 76 | class VulkanGame {
|
---|
[e8ebc76] | 77 | public:
|
---|
[34bdf3a] | 78 | VulkanGame(int maxFramesInFlight);
|
---|
[99d44b2] | 79 | ~VulkanGame();
|
---|
[0df3c9a] | 80 |
|
---|
[b6e60b4] | 81 | void run(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 82 |
|
---|
| 83 | private:
|
---|
[34bdf3a] | 84 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
| 85 |
|
---|
[5ab1b20] | 86 | const float NEAR_CLIP = 0.1f;
|
---|
| 87 | const float FAR_CLIP = 100.0f;
|
---|
[60578ce] | 88 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
[5ab1b20] | 89 |
|
---|
[15104a8] | 90 | vec3 cam_pos;
|
---|
| 91 |
|
---|
[0df3c9a] | 92 | GameGui* gui;
|
---|
[c559904] | 93 |
|
---|
| 94 | SDL_version sdlVersion;
|
---|
[b794178] | 95 | SDL_Window* window = nullptr;
|
---|
| 96 | SDL_Renderer* renderer = nullptr;
|
---|
| 97 |
|
---|
| 98 | SDL_Texture* uiOverlay = nullptr;
|
---|
[c1d9b2a] | 99 |
|
---|
| 100 | VkInstance instance;
|
---|
| 101 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
[fe5c3ba] | 102 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
[90a424f] | 103 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
[c1c2021] | 104 | VkDevice device;
|
---|
| 105 |
|
---|
| 106 | VkQueue graphicsQueue;
|
---|
| 107 | VkQueue presentQueue;
|
---|
[0df3c9a] | 108 |
|
---|
[502bd0b] | 109 | VkSwapchainKHR swapChain;
|
---|
| 110 | vector<VkImage> swapChainImages;
|
---|
| 111 | VkFormat swapChainImageFormat;
|
---|
[603b5bc] | 112 | VkExtent2D swapChainExtent;
|
---|
[f94eea9] | 113 | vector<VkImageView> swapChainImageViews;
|
---|
[603b5bc] | 114 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
[fa9fa1c] | 115 |
|
---|
[6fc24c7] | 116 | VkRenderPass renderPass;
|
---|
[fa9fa1c] | 117 | VkCommandPool commandPool;
|
---|
[603b5bc] | 118 | vector<VkCommandBuffer> commandBuffers;
|
---|
[502bd0b] | 119 |
|
---|
[603b5bc] | 120 | VulkanImage depthImage;
|
---|
[b794178] | 121 |
|
---|
| 122 | VkSampler textureSampler;
|
---|
| 123 |
|
---|
[0fe8433] | 124 | VulkanImage floorTextureImage;
|
---|
| 125 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 126 |
|
---|
| 127 | VulkanImage sdlOverlayImage;
|
---|
| 128 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
| 129 |
|
---|
| 130 | TTF_Font* font;
|
---|
| 131 | SDL_Texture* fontSDLTexture;
|
---|
| 132 |
|
---|
| 133 | SDL_Texture* imageSDLTexture;
|
---|
| 134 |
|
---|
| 135 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
| 136 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
| 137 | vector<VkFence> inFlightFences;
|
---|
| 138 |
|
---|
| 139 | size_t currentFrame;
|
---|
| 140 |
|
---|
| 141 | bool framebufferResized;
|
---|
| 142 |
|
---|
[055750a] | 143 | // TODO: I should probably rename the uniformBuffer* and storageBuffer*
|
---|
| 144 | // variables to better reflect the data they hold
|
---|
| 145 |
|
---|
[3e8cc8b] | 146 | // TODO: Create a struct that holds the buffers, memory, and info objects (Probably in VulkanUtils)
|
---|
| 147 |
|
---|
[0fe8433] | 148 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
---|
| 149 |
|
---|
| 150 | vector<SceneObject<OverlayVertex>> overlayObjects;
|
---|
| 151 |
|
---|
| 152 | // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
|
---|
| 153 |
|
---|
| 154 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
| 155 |
|
---|
| 156 | vector<SceneObject<ModelVertex>> modelObjects;
|
---|
| 157 |
|
---|
[055750a] | 158 | vector<VkBuffer> uniformBuffers_scenePipeline;
|
---|
| 159 | vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
|
---|
| 160 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
|
---|
[b794178] | 161 |
|
---|
[055750a] | 162 | vector<VkBuffer> storageBuffers_scenePipeline;
|
---|
| 163 | vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
|
---|
| 164 | vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
|
---|
[f97c5e7] | 165 |
|
---|
[0fe8433] | 166 | UBO_VP_mats object_VP_mats;
|
---|
| 167 | SBO_SceneObject so_Object;
|
---|
| 168 |
|
---|
| 169 | GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
|
---|
| 170 |
|
---|
| 171 | vector<SceneObject<ShipVertex>> shipObjects;
|
---|
| 172 |
|
---|
[3782d66] | 173 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
| 174 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
| 175 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
| 176 |
|
---|
[055750a] | 177 | vector<VkBuffer> storageBuffers_shipPipeline;
|
---|
| 178 | vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
|
---|
| 179 | vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
|
---|
| 180 |
|
---|
[0fe8433] | 181 | UBO_VP_mats ship_VP_mats;
|
---|
| 182 | SBO_SceneObject so_Ship;
|
---|
[0e09340] | 183 |
|
---|
[3e8cc8b] | 184 | GraphicsPipeline_Vulkan<AsteroidVertex> asteroidPipeline;
|
---|
| 185 |
|
---|
| 186 | vector<SceneObject<AsteroidVertex>> asteroidObjects;
|
---|
| 187 |
|
---|
| 188 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
| 189 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
| 190 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
| 191 |
|
---|
| 192 | vector<VkBuffer> storageBuffers_asteroidPipeline;
|
---|
| 193 | vector<VkDeviceMemory> storageBuffersMemory_asteroidPipeline;
|
---|
| 194 | vector<VkDescriptorBufferInfo> storageBufferInfoList_asteroidPipeline;
|
---|
| 195 |
|
---|
| 196 | UBO_VP_mats asteroid_VP_mats;
|
---|
| 197 | SBO_Asteroid so_Asteroid;
|
---|
| 198 |
|
---|
[4ece3bf] | 199 | Uint64 curTime, prevTime;
|
---|
| 200 | double elapsedTime;
|
---|
| 201 |
|
---|
[b6e60b4] | 202 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 203 | void initVulkan();
|
---|
[f97c5e7] | 204 | void initGraphicsPipelines();
|
---|
[15104a8] | 205 | void initMatrices();
|
---|
[0df3c9a] | 206 | void mainLoop();
|
---|
[8e02b6b] | 207 | void updateScene(uint32_t currentImage);
|
---|
[a0c5f28] | 208 | void renderUI();
|
---|
| 209 | void renderScene();
|
---|
[0df3c9a] | 210 | void cleanup();
|
---|
[c1d9b2a] | 211 |
|
---|
| 212 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
| 213 | void setupDebugMessenger();
|
---|
| 214 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
[90a424f] | 215 | void createVulkanSurface();
|
---|
[fe5c3ba] | 216 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[fa9fa1c] | 217 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
[c1c2021] | 218 | void createLogicalDevice(
|
---|
| 219 | const vector<const char*> validationLayers,
|
---|
| 220 | const vector<const char*>& deviceExtensions);
|
---|
[502bd0b] | 221 | void createSwapChain();
|
---|
[f94eea9] | 222 | void createImageViews();
|
---|
[6fc24c7] | 223 | void createRenderPass();
|
---|
| 224 | VkFormat findDepthFormat();
|
---|
[fa9fa1c] | 225 | void createCommandPool();
|
---|
[603b5bc] | 226 | void createImageResources();
|
---|
| 227 |
|
---|
[b794178] | 228 | void createTextureSampler();
|
---|
[603b5bc] | 229 | void createFramebuffers();
|
---|
| 230 | void createCommandBuffers();
|
---|
[34bdf3a] | 231 | void createSyncObjects();
|
---|
[f94eea9] | 232 |
|
---|
[0fe8433] | 233 | template<class VertexType>
|
---|
| 234 | void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
| 235 | const vector<VertexType>& vertices, vector<uint16_t> indices);
|
---|
| 236 |
|
---|
[06d959f] | 237 | template<class VertexType>
|
---|
| 238 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 239 |
|
---|
[cf727ca] | 240 | template<class VertexType>
|
---|
| 241 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 242 |
|
---|
[a79be34] | 243 | template<class VertexType>
|
---|
| 244 | vector<VertexType> centerObject(vector<VertexType> vertices);
|
---|
| 245 |
|
---|
[cd1cb0f] | 246 | template<class VertexType>
|
---|
| 247 | void transformObject(SceneObject<VertexType>& obj, mat4 mat);
|
---|
| 248 |
|
---|
[055750a] | 249 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
| 250 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
[f97c5e7] | 251 |
|
---|
[d2d9286] | 252 | void recreateSwapChain();
|
---|
| 253 |
|
---|
[c1c2021] | 254 | void cleanupSwapChain();
|
---|
[c1d9b2a] | 255 |
|
---|
| 256 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 257 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 258 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 259 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 260 | void* pUserData);
|
---|
[e8ebc76] | 261 | };
|
---|
| 262 |
|
---|
[0fe8433] | 263 | template<class VertexType>
|
---|
| 264 | void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
| 265 | const vector<VertexType>& vertices, vector<uint16_t> indices) {
|
---|
| 266 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 267 |
|
---|
| 268 | for (uint16_t& idx : indices) {
|
---|
| 269 | idx += numVertices;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
|
---|
| 273 |
|
---|
| 274 | pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[06d959f] | 277 | template<class VertexType>
|
---|
| 278 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 279 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 280 | vec3 p1 = vertices[i].pos;
|
---|
| 281 | vec3 p2 = vertices[i+1].pos;
|
---|
| 282 | vec3 p3 = vertices[i+2].pos;
|
---|
| 283 |
|
---|
[a79be34] | 284 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
[06d959f] | 285 |
|
---|
| 286 | // Add the same normal for all 3 vertices
|
---|
| 287 | vertices[i].normal = normal;
|
---|
| 288 | vertices[i+1].normal = normal;
|
---|
| 289 | vertices[i+2].normal = normal;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | return vertices;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[cf727ca] | 295 | template<class VertexType>
|
---|
| 296 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 297 | for (VertexType& vertex : vertices) {
|
---|
| 298 | vertex.objIndex = objIndex;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | return vertices;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[a79be34] | 304 | template<class VertexType>
|
---|
| 305 | vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
|
---|
| 306 | float min_x = vertices[0].pos.x;
|
---|
| 307 | float max_x = vertices[0].pos.x;
|
---|
| 308 | float min_y = vertices[0].pos.y;
|
---|
| 309 | float max_y = vertices[0].pos.y;
|
---|
| 310 | float min_z = vertices[0].pos.z;
|
---|
| 311 | float max_z = vertices[0].pos.z;
|
---|
| 312 |
|
---|
| 313 | // start from the second point
|
---|
| 314 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
| 315 | if (min_x > vertices[i].pos.x) {
|
---|
| 316 | min_x = vertices[i].pos.x;
|
---|
| 317 | } else if (max_x < vertices[i].pos.x) {
|
---|
| 318 | max_x = vertices[i].pos.x;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | if (min_y > vertices[i].pos.y) {
|
---|
| 322 | min_y = vertices[i].pos.y;
|
---|
| 323 | } else if (max_y < vertices[i].pos.y) {
|
---|
| 324 | max_y = vertices[i].pos.y;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | if (min_z > vertices[i].pos.z) {
|
---|
| 328 | min_z = vertices[i].pos.z;
|
---|
| 329 | } else if (max_z < vertices[i].pos.z) {
|
---|
| 330 | max_z = vertices[i].pos.z;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 335 |
|
---|
| 336 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 337 | vertices[i].pos -= center;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | return vertices;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[cd1cb0f] | 343 | template<class VertexType>
|
---|
| 344 | void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
|
---|
| 345 | obj.model_transform = mat * obj.model_transform;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[99d44b2] | 348 | #endif // _VULKAN_GAME_H |
---|