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