| 1 | #ifndef _VULKAN_GAME_H
|
|---|
| 2 | #define _VULKAN_GAME_H
|
|---|
| 3 |
|
|---|
| 4 | #include <glm/glm.hpp>
|
|---|
| 5 | #include <glm/gtc/matrix_transform.hpp>
|
|---|
| 6 |
|
|---|
| 7 | #include "game-gui-sdl.hpp"
|
|---|
| 8 | #include "graphics-pipeline_vulkan.hpp"
|
|---|
| 9 |
|
|---|
| 10 | #include "vulkan-utils.hpp"
|
|---|
| 11 |
|
|---|
| 12 | using namespace glm;
|
|---|
| 13 |
|
|---|
| 14 | #ifdef NDEBUG
|
|---|
| 15 | const bool ENABLE_VALIDATION_LAYERS = false;
|
|---|
| 16 | #else
|
|---|
| 17 | const bool ENABLE_VALIDATION_LAYERS = true;
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | struct ModelVertex {
|
|---|
| 21 | vec3 pos;
|
|---|
| 22 | vec3 color;
|
|---|
| 23 | vec2 texCoord;
|
|---|
| 24 | };
|
|---|
| 25 |
|
|---|
| 26 | struct OverlayVertex {
|
|---|
| 27 | vec3 pos;
|
|---|
| 28 | vec2 texCoord;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | struct ShipVertex {
|
|---|
| 32 | vec3 pos;
|
|---|
| 33 | vec3 color;
|
|---|
| 34 | vec3 normal;
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | struct UBO_MvpMat {
|
|---|
| 38 | alignas(16) mat4 model;
|
|---|
| 39 | alignas(16) mat4 view;
|
|---|
| 40 | alignas(16) mat4 proj;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | class VulkanGame {
|
|---|
| 44 | public:
|
|---|
| 45 | VulkanGame(int maxFramesInFlight);
|
|---|
| 46 | ~VulkanGame();
|
|---|
| 47 |
|
|---|
| 48 | void run(int width, int height, unsigned char guiFlags);
|
|---|
| 49 |
|
|---|
| 50 | private:
|
|---|
| 51 | const int MAX_FRAMES_IN_FLIGHT;
|
|---|
| 52 |
|
|---|
| 53 | const float NEAR_CLIP = 0.1f;
|
|---|
| 54 | const float FAR_CLIP = 100.0f;
|
|---|
| 55 | const float FOV_ANGLE = 67.0f;
|
|---|
| 56 |
|
|---|
| 57 | vec3 cam_pos;
|
|---|
| 58 |
|
|---|
| 59 | UBO_MvpMat modelMvpMats;
|
|---|
| 60 | UBO_MvpMat shipMvpMats;
|
|---|
| 61 |
|
|---|
| 62 | GameGui* gui;
|
|---|
| 63 |
|
|---|
| 64 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
|---|
| 65 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
|---|
| 66 | GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
|
|---|
| 67 |
|
|---|
| 68 | SDL_version sdlVersion;
|
|---|
| 69 | SDL_Window* window = nullptr;
|
|---|
| 70 | SDL_Renderer* renderer = nullptr;
|
|---|
| 71 |
|
|---|
| 72 | SDL_Texture* uiOverlay = nullptr;
|
|---|
| 73 |
|
|---|
| 74 | VkInstance instance;
|
|---|
| 75 | VkDebugUtilsMessengerEXT debugMessenger;
|
|---|
| 76 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
|---|
| 77 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|---|
| 78 | VkDevice device;
|
|---|
| 79 |
|
|---|
| 80 | VkQueue graphicsQueue;
|
|---|
| 81 | VkQueue presentQueue;
|
|---|
| 82 |
|
|---|
| 83 | VkSwapchainKHR swapChain;
|
|---|
| 84 | vector<VkImage> swapChainImages;
|
|---|
| 85 | VkFormat swapChainImageFormat;
|
|---|
| 86 | VkExtent2D swapChainExtent;
|
|---|
| 87 | vector<VkImageView> swapChainImageViews;
|
|---|
| 88 | vector<VkFramebuffer> swapChainFramebuffers;
|
|---|
| 89 |
|
|---|
| 90 | VkRenderPass renderPass;
|
|---|
| 91 | VkCommandPool commandPool;
|
|---|
| 92 | vector<VkCommandBuffer> commandBuffers;
|
|---|
| 93 |
|
|---|
| 94 | VulkanImage depthImage;
|
|---|
| 95 |
|
|---|
| 96 | VkSampler textureSampler;
|
|---|
| 97 |
|
|---|
| 98 | // These are currently to store the MVP matrix
|
|---|
| 99 | // I should figure out if it makes sense to use them for other uniforms in the future
|
|---|
| 100 | // If not, I should rename them to better indicate their purpose.
|
|---|
| 101 | vector<VkBuffer> uniformBuffers;
|
|---|
| 102 | vector<VkDeviceMemory> uniformBuffersMemory;
|
|---|
| 103 |
|
|---|
| 104 | vector<VkDescriptorBufferInfo> uniformBufferInfoList;
|
|---|
| 105 |
|
|---|
| 106 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
|---|
| 107 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
|---|
| 108 |
|
|---|
| 109 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
|---|
| 110 |
|
|---|
| 111 | VulkanImage floorTextureImage;
|
|---|
| 112 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
|---|
| 113 |
|
|---|
| 114 | VulkanImage sdlOverlayImage;
|
|---|
| 115 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
|---|
| 116 |
|
|---|
| 117 | TTF_Font* font;
|
|---|
| 118 | SDL_Texture* fontSDLTexture;
|
|---|
| 119 |
|
|---|
| 120 | SDL_Texture* imageSDLTexture;
|
|---|
| 121 |
|
|---|
| 122 | vector<VkSemaphore> imageAvailableSemaphores;
|
|---|
| 123 | vector<VkSemaphore> renderFinishedSemaphores;
|
|---|
| 124 | vector<VkFence> inFlightFences;
|
|---|
| 125 |
|
|---|
| 126 | size_t currentFrame;
|
|---|
| 127 |
|
|---|
| 128 | bool framebufferResized;
|
|---|
| 129 |
|
|---|
| 130 | bool initWindow(int width, int height, unsigned char guiFlags);
|
|---|
| 131 | void initVulkan();
|
|---|
| 132 | void initGraphicsPipelines();
|
|---|
| 133 | void initMatrices();
|
|---|
| 134 | void mainLoop();
|
|---|
| 135 | void updateScene(uint32_t currentImage);
|
|---|
| 136 | void renderUI();
|
|---|
| 137 | void renderScene();
|
|---|
| 138 | void cleanup();
|
|---|
| 139 |
|
|---|
| 140 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
|---|
| 141 | void setupDebugMessenger();
|
|---|
| 142 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
|---|
| 143 | void createVulkanSurface();
|
|---|
| 144 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
|---|
| 145 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
|---|
| 146 | void createLogicalDevice(
|
|---|
| 147 | const vector<const char*> validationLayers,
|
|---|
| 148 | const vector<const char*>& deviceExtensions);
|
|---|
| 149 | void createSwapChain();
|
|---|
| 150 | void createImageViews();
|
|---|
| 151 | void createRenderPass();
|
|---|
| 152 | VkFormat findDepthFormat();
|
|---|
| 153 | void createCommandPool();
|
|---|
| 154 | void createImageResources();
|
|---|
| 155 |
|
|---|
| 156 | void createTextureSampler();
|
|---|
| 157 | void createFramebuffers();
|
|---|
| 158 | void createCommandBuffers();
|
|---|
| 159 | void createSyncObjects();
|
|---|
| 160 |
|
|---|
| 161 | template<class VertexType>
|
|---|
| 162 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
|---|
| 163 |
|
|---|
| 164 | template<class UniformType>
|
|---|
| 165 | void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
|---|
| 166 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
|---|
| 167 |
|
|---|
| 168 | void recreateSwapChain();
|
|---|
| 169 |
|
|---|
| 170 | void cleanupSwapChain();
|
|---|
| 171 |
|
|---|
| 172 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
|---|
| 173 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|---|
| 174 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|---|
| 175 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|---|
| 176 | void* pUserData);
|
|---|
| 177 | };
|
|---|
| 178 |
|
|---|
| 179 | template<class UniformType>
|
|---|
| 180 | void VulkanGame::createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
|---|
| 181 | vector<VkDescriptorBufferInfo>& bufferInfoList) {
|
|---|
| 182 | buffers.resize(swapChainImages.size());
|
|---|
| 183 | buffersMemory.resize(swapChainImages.size());
|
|---|
| 184 | bufferInfoList.resize(swapChainImages.size());
|
|---|
| 185 |
|
|---|
| 186 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
|---|
| 187 | VulkanUtils::createBuffer(device, physicalDevice, sizeof(UniformType), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|---|
| 188 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|---|
| 189 | buffers[i], buffersMemory[i]);
|
|---|
| 190 |
|
|---|
| 191 | bufferInfoList[i].buffer = buffers[i];
|
|---|
| 192 | bufferInfoList[i].offset = 0;
|
|---|
| 193 | bufferInfoList[i].range = sizeof(UniformType);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | template<class VertexType>
|
|---|
| 198 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
|---|
| 199 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
|---|
| 200 | vec3 p1 = vertices[i].pos;
|
|---|
| 201 | vec3 p2 = vertices[i+1].pos;
|
|---|
| 202 | vec3 p3 = vertices[i+2].pos;
|
|---|
| 203 |
|
|---|
| 204 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
|---|
| 205 | normal.z = -normal.z;
|
|---|
| 206 |
|
|---|
| 207 | // Add the same normal for all 3 vertices
|
|---|
| 208 | vertices[i].normal = normal;
|
|---|
| 209 | vertices[i+1].normal = normal;
|
|---|
| 210 | vertices[i+2].normal = normal;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | return vertices;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | #endif // _VULKAN_GAME_H
|
|---|