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 | };
|
---|
35 |
|
---|
36 | struct UBO_MvpMat {
|
---|
37 | alignas(16) mat4 model;
|
---|
38 | alignas(16) mat4 view;
|
---|
39 | alignas(16) mat4 proj;
|
---|
40 | };
|
---|
41 |
|
---|
42 | class VulkanGame {
|
---|
43 | public:
|
---|
44 | VulkanGame(int maxFramesInFlight);
|
---|
45 | ~VulkanGame();
|
---|
46 |
|
---|
47 | void run(int width, int height, unsigned char guiFlags);
|
---|
48 |
|
---|
49 | private:
|
---|
50 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
51 |
|
---|
52 | const float NEAR_CLIP = 0.1f;
|
---|
53 | const float FAR_CLIP = 100.0f;
|
---|
54 | const float FOV_ANGLE = 67.0f;
|
---|
55 |
|
---|
56 | vec3 cam_pos;
|
---|
57 |
|
---|
58 | UBO_MvpMat modelMvpMats;
|
---|
59 | UBO_MvpMat shipMvpMats;
|
---|
60 |
|
---|
61 | GameGui* gui;
|
---|
62 |
|
---|
63 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
64 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
---|
65 | GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
|
---|
66 |
|
---|
67 | SDL_version sdlVersion;
|
---|
68 | SDL_Window* window = nullptr;
|
---|
69 | SDL_Renderer* renderer = nullptr;
|
---|
70 |
|
---|
71 | SDL_Texture* uiOverlay = nullptr;
|
---|
72 |
|
---|
73 | VkInstance instance;
|
---|
74 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
75 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
76 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
77 | VkDevice device;
|
---|
78 |
|
---|
79 | VkQueue graphicsQueue;
|
---|
80 | VkQueue presentQueue;
|
---|
81 |
|
---|
82 | VkSwapchainKHR swapChain;
|
---|
83 | vector<VkImage> swapChainImages;
|
---|
84 | VkFormat swapChainImageFormat;
|
---|
85 | VkExtent2D swapChainExtent;
|
---|
86 | vector<VkImageView> swapChainImageViews;
|
---|
87 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
88 |
|
---|
89 | VkRenderPass renderPass;
|
---|
90 | VkCommandPool commandPool;
|
---|
91 | vector<VkCommandBuffer> commandBuffers;
|
---|
92 |
|
---|
93 | VulkanImage depthImage;
|
---|
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 |
|
---|
103 | vector<VkDescriptorBufferInfo> uniformBufferInfoList;
|
---|
104 |
|
---|
105 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
106 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
107 |
|
---|
108 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
109 |
|
---|
110 | VulkanImage floorTextureImage;
|
---|
111 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
112 |
|
---|
113 | VulkanImage sdlOverlayImage;
|
---|
114 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
115 |
|
---|
116 | TTF_Font* font;
|
---|
117 | SDL_Texture* fontSDLTexture;
|
---|
118 |
|
---|
119 | SDL_Texture* imageSDLTexture;
|
---|
120 |
|
---|
121 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
122 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
123 | vector<VkFence> inFlightFences;
|
---|
124 |
|
---|
125 | size_t currentFrame;
|
---|
126 |
|
---|
127 | bool framebufferResized;
|
---|
128 |
|
---|
129 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
130 | void initVulkan();
|
---|
131 | void initGraphicsPipelines();
|
---|
132 | void initMatrices();
|
---|
133 | void mainLoop();
|
---|
134 | void updateScene(uint32_t currentImage);
|
---|
135 | void renderUI();
|
---|
136 | void renderScene();
|
---|
137 | void cleanup();
|
---|
138 |
|
---|
139 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
140 | void setupDebugMessenger();
|
---|
141 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
142 | void createVulkanSurface();
|
---|
143 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
144 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
145 | void createLogicalDevice(
|
---|
146 | const vector<const char*> validationLayers,
|
---|
147 | const vector<const char*>& deviceExtensions);
|
---|
148 | void createSwapChain();
|
---|
149 | void createImageViews();
|
---|
150 | void createRenderPass();
|
---|
151 | VkFormat findDepthFormat();
|
---|
152 | void createCommandPool();
|
---|
153 | void createImageResources();
|
---|
154 |
|
---|
155 | void createTextureSampler();
|
---|
156 | void createFramebuffers();
|
---|
157 | void createCommandBuffers();
|
---|
158 | void createSyncObjects();
|
---|
159 |
|
---|
160 | template<class UniformType>
|
---|
161 | void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
162 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
163 |
|
---|
164 | void recreateSwapChain();
|
---|
165 |
|
---|
166 | void cleanupSwapChain();
|
---|
167 |
|
---|
168 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
169 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
170 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
171 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
172 | void* pUserData);
|
---|
173 | };
|
---|
174 |
|
---|
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 |
|
---|
193 | #endif // _VULKAN_GAME_H
|
---|