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