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