1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #include <chrono>
|
---|
5 |
|
---|
6 | #define GLM_FORCE_RADIANS
|
---|
7 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
8 | #define GLM_FORCE_RIGHT_HANDED
|
---|
9 |
|
---|
10 | #include <glm/glm.hpp>
|
---|
11 | #include <glm/gtc/matrix_transform.hpp>
|
---|
12 |
|
---|
13 | #include "game-gui-sdl.hpp"
|
---|
14 | #include "graphics-pipeline_vulkan.hpp"
|
---|
15 |
|
---|
16 | #include "vulkan-utils.hpp"
|
---|
17 |
|
---|
18 | using namespace glm;
|
---|
19 | using namespace std::chrono;
|
---|
20 |
|
---|
21 | #ifdef NDEBUG
|
---|
22 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
23 | #else
|
---|
24 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | struct OverlayVertex {
|
---|
28 | vec3 pos;
|
---|
29 | vec2 texCoord;
|
---|
30 | };
|
---|
31 |
|
---|
32 | struct ModelVertex {
|
---|
33 | vec3 pos;
|
---|
34 | vec3 color;
|
---|
35 | vec2 texCoord;
|
---|
36 | unsigned int objIndex;
|
---|
37 | };
|
---|
38 |
|
---|
39 | struct ShipVertex {
|
---|
40 | vec3 pos;
|
---|
41 | vec3 color;
|
---|
42 | vec3 normal;
|
---|
43 | unsigned int objIndex;
|
---|
44 | };
|
---|
45 |
|
---|
46 | struct AsteroidVertex {
|
---|
47 | vec3 pos;
|
---|
48 | vec3 color;
|
---|
49 | vec3 normal;
|
---|
50 | unsigned int objIndex;
|
---|
51 | };
|
---|
52 |
|
---|
53 | struct UBO_VP_mats {
|
---|
54 | alignas(16) mat4 view;
|
---|
55 | alignas(16) mat4 proj;
|
---|
56 | };
|
---|
57 |
|
---|
58 | struct SSBO_ModelObject {
|
---|
59 | alignas(16) mat4 model;
|
---|
60 | };
|
---|
61 |
|
---|
62 | struct SSBO_Asteroid {
|
---|
63 | alignas(16) mat4 model;
|
---|
64 | alignas(4) float hp;
|
---|
65 | alignas(4) unsigned int deleted;
|
---|
66 | };
|
---|
67 |
|
---|
68 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
69 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
70 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
71 | template<class VertexType, class SSBOType>
|
---|
72 | struct SceneObject {
|
---|
73 | vector<VertexType> vertices;
|
---|
74 | vector<uint16_t> indices;
|
---|
75 | SSBOType ssbo;
|
---|
76 |
|
---|
77 | mat4 model_base;
|
---|
78 | mat4 model_transform;
|
---|
79 |
|
---|
80 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
81 | // parent class
|
---|
82 | vec3 center; // currently only matters for asteroids
|
---|
83 | float radius; // currently only matters for asteroids
|
---|
84 | };
|
---|
85 |
|
---|
86 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
87 | // Could probably use the same approach to make indices optional
|
---|
88 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
89 | // them mamdatory
|
---|
90 |
|
---|
91 | class VulkanGame {
|
---|
92 | public:
|
---|
93 | VulkanGame(int maxFramesInFlight);
|
---|
94 | ~VulkanGame();
|
---|
95 |
|
---|
96 | void run(int width, int height, unsigned char guiFlags);
|
---|
97 |
|
---|
98 | private:
|
---|
99 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
100 |
|
---|
101 | const float NEAR_CLIP = 0.1f;
|
---|
102 | const float FAR_CLIP = 100.0f;
|
---|
103 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
104 |
|
---|
105 | vec3 cam_pos;
|
---|
106 |
|
---|
107 | GameGui* gui;
|
---|
108 |
|
---|
109 | SDL_version sdlVersion;
|
---|
110 | SDL_Window* window = nullptr;
|
---|
111 | SDL_Renderer* renderer = nullptr;
|
---|
112 |
|
---|
113 | SDL_Texture* uiOverlay = nullptr;
|
---|
114 |
|
---|
115 | VkInstance instance;
|
---|
116 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
117 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
118 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
119 | VkDevice device;
|
---|
120 |
|
---|
121 | VkQueue graphicsQueue;
|
---|
122 | VkQueue presentQueue;
|
---|
123 |
|
---|
124 | VkSwapchainKHR swapChain;
|
---|
125 | vector<VkImage> swapChainImages;
|
---|
126 | VkFormat swapChainImageFormat;
|
---|
127 | VkExtent2D swapChainExtent;
|
---|
128 | vector<VkImageView> swapChainImageViews;
|
---|
129 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
130 |
|
---|
131 | VkRenderPass renderPass;
|
---|
132 | VkCommandPool commandPool;
|
---|
133 | vector<VkCommandBuffer> commandBuffers;
|
---|
134 |
|
---|
135 | VulkanImage depthImage;
|
---|
136 |
|
---|
137 | VkSampler textureSampler;
|
---|
138 |
|
---|
139 | VulkanImage sdlOverlayImage;
|
---|
140 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
141 |
|
---|
142 | VulkanImage floorTextureImage;
|
---|
143 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
144 |
|
---|
145 | TTF_Font* font;
|
---|
146 | SDL_Texture* fontSDLTexture;
|
---|
147 |
|
---|
148 | SDL_Texture* imageSDLTexture;
|
---|
149 |
|
---|
150 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
151 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
152 | vector<VkFence> inFlightFences;
|
---|
153 |
|
---|
154 | size_t currentFrame;
|
---|
155 |
|
---|
156 | bool framebufferResized;
|
---|
157 |
|
---|
158 | mat4 viewMat, projMat;
|
---|
159 |
|
---|
160 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
---|
161 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
---|
162 |
|
---|
163 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
164 | // per pipeline.
|
---|
165 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
166 | // the objects vector, the ubo, and the ssbo
|
---|
167 |
|
---|
168 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
169 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
170 |
|
---|
171 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
172 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
173 |
|
---|
174 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
175 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
176 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
177 |
|
---|
178 | UBO_VP_mats object_VP_mats;
|
---|
179 |
|
---|
180 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
181 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
182 |
|
---|
183 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
184 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
185 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
186 |
|
---|
187 | UBO_VP_mats ship_VP_mats;
|
---|
188 |
|
---|
189 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
190 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
191 |
|
---|
192 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
193 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
194 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
195 |
|
---|
196 | UBO_VP_mats asteroid_VP_mats;
|
---|
197 |
|
---|
198 | time_point<steady_clock> startTime;
|
---|
199 | float curTime, prevTime, elapsedTime;
|
---|
200 |
|
---|
201 | float shipSpeed = 0.5f;
|
---|
202 | float asteroidSpeed = 2.0f;
|
---|
203 |
|
---|
204 | float spawnRate_asteroid = 0.5;
|
---|
205 | float lastSpawn_asteroid;
|
---|
206 |
|
---|
207 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
208 | void initVulkan();
|
---|
209 | void initGraphicsPipelines();
|
---|
210 | void initMatrices();
|
---|
211 | void mainLoop();
|
---|
212 | void updateScene(uint32_t currentImage);
|
---|
213 | void renderUI();
|
---|
214 | void renderScene();
|
---|
215 | void cleanup();
|
---|
216 |
|
---|
217 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
218 | void setupDebugMessenger();
|
---|
219 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
220 | void createVulkanSurface();
|
---|
221 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
222 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
223 | void createLogicalDevice(
|
---|
224 | const vector<const char*> validationLayers,
|
---|
225 | const vector<const char*>& deviceExtensions);
|
---|
226 | void createSwapChain();
|
---|
227 | void createImageViews();
|
---|
228 | void createRenderPass();
|
---|
229 | VkFormat findDepthFormat();
|
---|
230 | void createCommandPool();
|
---|
231 | void createImageResources();
|
---|
232 |
|
---|
233 | void createTextureSampler();
|
---|
234 | void createFramebuffers();
|
---|
235 | void createCommandBuffers();
|
---|
236 | void createSyncObjects();
|
---|
237 |
|
---|
238 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
239 | // stop using objects.back() to access the object that was just created
|
---|
240 | template<class VertexType, class SSBOType>
|
---|
241 | SceneObject<VertexType, SSBOType>& addObject(
|
---|
242 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
243 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
244 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
245 | bool pipelinesCreated);
|
---|
246 |
|
---|
247 | template<class VertexType, class SSBOType>
|
---|
248 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
249 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
250 |
|
---|
251 | template<class VertexType>
|
---|
252 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
253 |
|
---|
254 | template<class VertexType>
|
---|
255 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
256 |
|
---|
257 | template<class VertexType, class SSBOType>
|
---|
258 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
259 |
|
---|
260 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
261 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
262 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
263 |
|
---|
264 | void recreateSwapChain();
|
---|
265 |
|
---|
266 | void cleanupSwapChain();
|
---|
267 |
|
---|
268 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
269 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
270 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
271 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
272 | void* pUserData);
|
---|
273 | };
|
---|
274 |
|
---|
275 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
276 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
277 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
---|
278 |
|
---|
279 | // TODO: Maybe return a reference to the object from this method if I decide that updating it
|
---|
280 | // immediately after creation is a good idea (such as setting model_base)
|
---|
281 | // Currently, model_base is set like this in a few places and the radius is set for asteroids
|
---|
282 | // to account for scaling
|
---|
283 | template<class VertexType, class SSBOType>
|
---|
284 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
285 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
286 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
287 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
288 | bool pipelinesCreated) {
|
---|
289 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
290 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
291 | size_t numVertices = pipeline.getNumVertices();
|
---|
292 |
|
---|
293 | for (uint16_t& idx : indices) {
|
---|
294 | idx += numVertices;
|
---|
295 | }
|
---|
296 |
|
---|
297 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
|
---|
298 |
|
---|
299 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
300 | centerObject(obj);
|
---|
301 |
|
---|
302 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
303 | this->commandPool, this->graphicsQueue);
|
---|
304 |
|
---|
305 | if (pipelinesCreated) {
|
---|
306 | vkDeviceWaitIdle(device);
|
---|
307 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
308 |
|
---|
309 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
---|
310 | // one SSBO is resized.
|
---|
311 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
---|
312 | // are created and then recreate each of the corresponding pipelines only once per frame
|
---|
313 | if (storageBufferResized) {
|
---|
314 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
315 | pipeline.createDescriptorPool(swapChainImages);
|
---|
316 | pipeline.createDescriptorSets(swapChainImages);
|
---|
317 | }
|
---|
318 |
|
---|
319 | createCommandBuffers();
|
---|
320 | }
|
---|
321 |
|
---|
322 | return obj;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
326 | template<class VertexType, class SSBOType>
|
---|
327 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
328 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
---|
329 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
---|
330 |
|
---|
331 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
332 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
333 |
|
---|
334 | pipeline.updateObject(index, obj.ssbo);
|
---|
335 | }
|
---|
336 |
|
---|
337 | template<class VertexType>
|
---|
338 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
339 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
340 | vec3 p1 = vertices[i].pos;
|
---|
341 | vec3 p2 = vertices[i+1].pos;
|
---|
342 | vec3 p3 = vertices[i+2].pos;
|
---|
343 |
|
---|
344 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
345 |
|
---|
346 | // Add the same normal for all 3 vertices
|
---|
347 | vertices[i].normal = normal;
|
---|
348 | vertices[i+1].normal = normal;
|
---|
349 | vertices[i+2].normal = normal;
|
---|
350 | }
|
---|
351 |
|
---|
352 | return vertices;
|
---|
353 | }
|
---|
354 |
|
---|
355 | template<class VertexType>
|
---|
356 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
357 | for (VertexType& vertex : vertices) {
|
---|
358 | vertex.objIndex = objIndex;
|
---|
359 | }
|
---|
360 |
|
---|
361 | return vertices;
|
---|
362 | }
|
---|
363 |
|
---|
364 | template<class VertexType, class SSBOType>
|
---|
365 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
366 | vector<VertexType>& vertices = object.vertices;
|
---|
367 |
|
---|
368 | float min_x = vertices[0].pos.x;
|
---|
369 | float max_x = vertices[0].pos.x;
|
---|
370 | float min_y = vertices[0].pos.y;
|
---|
371 | float max_y = vertices[0].pos.y;
|
---|
372 | float min_z = vertices[0].pos.z;
|
---|
373 | float max_z = vertices[0].pos.z;
|
---|
374 |
|
---|
375 | // start from the second point
|
---|
376 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
377 | vec3& pos = vertices[i].pos;
|
---|
378 |
|
---|
379 | if (min_x > pos.x) {
|
---|
380 | min_x = pos.x;
|
---|
381 | } else if (max_x < pos.x) {
|
---|
382 | max_x = pos.x;
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (min_y > pos.y) {
|
---|
386 | min_y = pos.y;
|
---|
387 | } else if (max_y < pos.y) {
|
---|
388 | max_y = pos.y;
|
---|
389 | }
|
---|
390 |
|
---|
391 | if (min_z > pos.z) {
|
---|
392 | min_z = pos.z;
|
---|
393 | } else if (max_z < pos.z) {
|
---|
394 | max_z = pos.z;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
399 |
|
---|
400 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
401 | vertices[i].pos -= center;
|
---|
402 | }
|
---|
403 |
|
---|
404 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
405 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
406 |
|
---|
407 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
408 | }
|
---|
409 |
|
---|
410 | #endif // _VULKAN_GAME_H
|
---|