1 | #ifndef _SDL_GAME_H
|
---|
2 | #define _SDL_GAME_H
|
---|
3 |
|
---|
4 | #include <chrono>
|
---|
5 | #include <map>
|
---|
6 | #include <vector>
|
---|
7 |
|
---|
8 | #include <vulkan/vulkan.h>
|
---|
9 |
|
---|
10 | #include <SDL2/SDL.h>
|
---|
11 |
|
---|
12 | #define GLM_FORCE_RADIANS
|
---|
13 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
14 | #define GLM_FORCE_RIGHT_HANDED
|
---|
15 |
|
---|
16 | #include <glm/glm.hpp>
|
---|
17 | #include <glm/gtc/matrix_transform.hpp>
|
---|
18 |
|
---|
19 | #include "IMGUI/imgui_impl_vulkan.h"
|
---|
20 |
|
---|
21 | #include "consts.hpp"
|
---|
22 | #include "game-gui-sdl.hpp"
|
---|
23 | #include "vulkan-utils.hpp"
|
---|
24 | #include "graphics-pipeline_vulkan.hpp"
|
---|
25 | #include "vulkan-buffer.hpp"
|
---|
26 |
|
---|
27 | using namespace glm;
|
---|
28 | using namespace std;
|
---|
29 | using namespace std::chrono;
|
---|
30 |
|
---|
31 | #define VulkanGame NewVulkanGame
|
---|
32 |
|
---|
33 | #ifdef NDEBUG
|
---|
34 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
35 | #else
|
---|
36 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | // TODO: Consider if there is a better way of dealing with all the vertex types and ssbo types, maybe
|
---|
40 | // by consolidating some and trying to keep new ones to a minimum
|
---|
41 |
|
---|
42 | struct ModelVertex {
|
---|
43 | vec3 pos;
|
---|
44 | vec3 color;
|
---|
45 | vec2 texCoord;
|
---|
46 | vec3 normal;
|
---|
47 | unsigned int objIndex;
|
---|
48 | };
|
---|
49 |
|
---|
50 | struct LaserVertex {
|
---|
51 | vec3 pos;
|
---|
52 | vec2 texCoord;
|
---|
53 | unsigned int objIndex;
|
---|
54 | };
|
---|
55 |
|
---|
56 | struct ExplosionVertex {
|
---|
57 | vec3 particleStartVelocity;
|
---|
58 | float particleStartTime;
|
---|
59 | unsigned int objIndex;
|
---|
60 | };
|
---|
61 |
|
---|
62 | // Currently using these as the dynamic UBO types as well
|
---|
63 | // TODO: Rename them to something more general
|
---|
64 |
|
---|
65 | struct SSBO_ModelObject {
|
---|
66 | alignas(16) mat4 model;
|
---|
67 | };
|
---|
68 |
|
---|
69 | struct SSBO_Asteroid {
|
---|
70 | alignas(16) mat4 model;
|
---|
71 | alignas(4) float hp;
|
---|
72 | alignas(4) unsigned int deleted;
|
---|
73 | };
|
---|
74 |
|
---|
75 | struct UBO_VP_mats {
|
---|
76 | alignas(16) mat4 view;
|
---|
77 | alignas(16) mat4 proj;
|
---|
78 | };
|
---|
79 |
|
---|
80 | // TODO: Use this struct for uniform buffers as well and probably combine it with the VulkanBuffer class
|
---|
81 | // Also, probably better to make this a vector of structs where each struct
|
---|
82 | // has a VkBuffer, VkDeviceMemory, and VkDescriptorBufferInfo
|
---|
83 | // TODO: Maybe change the structure here since VkDescriptorBufferInfo already stores a reference to the VkBuffer
|
---|
84 | struct BufferSet {
|
---|
85 | vector<VkBuffer> buffers;
|
---|
86 | vector<VkDeviceMemory> memory;
|
---|
87 | vector<VkDescriptorBufferInfo> infoSet;
|
---|
88 | VkBufferUsageFlags usages;
|
---|
89 | VkMemoryPropertyFlags properties;
|
---|
90 | };
|
---|
91 |
|
---|
92 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
93 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
94 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
95 | template<class VertexType>
|
---|
96 | struct SceneObject {
|
---|
97 | vector<VertexType> vertices;
|
---|
98 | vector<uint16_t> indices;
|
---|
99 |
|
---|
100 | mat4 model_base;
|
---|
101 | mat4 model_transform;
|
---|
102 |
|
---|
103 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
104 | // parent class
|
---|
105 | vec3 center; // currently only matters for asteroids
|
---|
106 | float radius; // currently only matters for asteroids
|
---|
107 |
|
---|
108 | // Move the targetAsteroid stuff out of this class since it is very specific to lasers
|
---|
109 | // and makes moving SceneObject into its own header file more problematic
|
---|
110 | SceneObject<ModelVertex>* targetAsteroid; // currently only used for lasers
|
---|
111 | };
|
---|
112 |
|
---|
113 | // TODO: Figure out how to include an optional ssbo parameter for each object
|
---|
114 | // Could probably use the same approach to make indices optional
|
---|
115 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
116 | // them mandatory
|
---|
117 |
|
---|
118 |
|
---|
119 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
---|
120 |
|
---|
121 | // TODO: Maybe move this to a different header
|
---|
122 |
|
---|
123 | enum UIValueType {
|
---|
124 | UIVALUE_INT,
|
---|
125 | UIVALUE_DOUBLE,
|
---|
126 | };
|
---|
127 |
|
---|
128 | struct UIValue {
|
---|
129 | UIValueType type;
|
---|
130 | string label;
|
---|
131 | void* value;
|
---|
132 |
|
---|
133 | UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
|
---|
134 | };
|
---|
135 |
|
---|
136 | class VulkanGame {
|
---|
137 |
|
---|
138 | public:
|
---|
139 |
|
---|
140 | VulkanGame();
|
---|
141 | ~VulkanGame();
|
---|
142 |
|
---|
143 | void run(int width, int height, unsigned char guiFlags);
|
---|
144 |
|
---|
145 | private:
|
---|
146 |
|
---|
147 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
148 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
149 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
150 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
151 | void* pUserData);
|
---|
152 |
|
---|
153 | // TODO: Maybe pass these in as parameters to some Camera class
|
---|
154 | const float NEAR_CLIP = 0.1f;
|
---|
155 | const float FAR_CLIP = 100.0f;
|
---|
156 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 deg
|
---|
157 |
|
---|
158 | bool done;
|
---|
159 |
|
---|
160 | vec3 cam_pos;
|
---|
161 |
|
---|
162 | // TODO: Good place to start using smart pointers
|
---|
163 | GameGui* gui;
|
---|
164 |
|
---|
165 | SDL_version sdlVersion;
|
---|
166 | SDL_Window* window;
|
---|
167 |
|
---|
168 | VkInstance instance;
|
---|
169 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
170 | VkSurfaceKHR vulkanSurface;
|
---|
171 | VkPhysicalDevice physicalDevice;
|
---|
172 | VkDevice device;
|
---|
173 |
|
---|
174 | VkQueue graphicsQueue;
|
---|
175 | VkQueue presentQueue;
|
---|
176 |
|
---|
177 | // TODO: Maybe make a swapchain struct for convenience
|
---|
178 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
---|
179 | VkPresentModeKHR swapChainPresentMode;
|
---|
180 | VkExtent2D swapChainExtent;
|
---|
181 | uint32_t swapChainMinImageCount;
|
---|
182 | uint32_t swapChainImageCount;
|
---|
183 |
|
---|
184 | VkSwapchainKHR swapChain;
|
---|
185 | vector<VkImage> swapChainImages;
|
---|
186 | vector<VkImageView> swapChainImageViews;
|
---|
187 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
188 |
|
---|
189 | VkRenderPass renderPass;
|
---|
190 |
|
---|
191 | VkCommandPool resourceCommandPool;
|
---|
192 |
|
---|
193 | vector<VkCommandPool> commandPools;
|
---|
194 | vector<VkCommandBuffer> commandBuffers;
|
---|
195 |
|
---|
196 | VulkanImage depthImage;
|
---|
197 |
|
---|
198 | // These are per frame
|
---|
199 | vector<VkSemaphore> imageAcquiredSemaphores;
|
---|
200 | vector<VkSemaphore> renderCompleteSemaphores;
|
---|
201 |
|
---|
202 | // These are per swap chain image
|
---|
203 | vector<VkFence> inFlightFences;
|
---|
204 |
|
---|
205 | uint32_t imageIndex;
|
---|
206 | uint32_t currentFrame;
|
---|
207 |
|
---|
208 | bool shouldRecreateSwapChain;
|
---|
209 |
|
---|
210 | VkSampler textureSampler;
|
---|
211 |
|
---|
212 | VulkanImage floorTextureImage;
|
---|
213 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
214 |
|
---|
215 | mat4 viewMat, projMat;
|
---|
216 |
|
---|
217 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
218 | VkDescriptorPool imguiDescriptorPool;
|
---|
219 |
|
---|
220 | // TODO: Probably restructure the GraphicsPipeline_Vulkan class based on what I learned about descriptors and textures
|
---|
221 | // while working on graphics-library. Double-check exactly what this was and note it down here.
|
---|
222 | // Basically, I think the point was that if I have several models that all use the same shaders and, therefore,
|
---|
223 | // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
|
---|
224 | // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
|
---|
225 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
226 |
|
---|
227 | BufferSet storageBuffers_modelPipeline;
|
---|
228 | VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
|
---|
229 | BufferSet uniformBuffers_modelPipeline;
|
---|
230 |
|
---|
231 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
232 | // per pipeline.
|
---|
233 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
234 | // the objects vector, the ubo, and the ssbo
|
---|
235 |
|
---|
236 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
237 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
238 |
|
---|
239 | vector<SceneObject<ModelVertex>> modelObjects;
|
---|
240 |
|
---|
241 | UBO_VP_mats object_VP_mats;
|
---|
242 |
|
---|
243 | /*** High-level vars ***/
|
---|
244 |
|
---|
245 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
246 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
247 |
|
---|
248 | map<string, vector<UIValue>> valueLists;
|
---|
249 |
|
---|
250 | int score;
|
---|
251 | float fps;
|
---|
252 |
|
---|
253 | // TODO: Make a separate singleton Timer class
|
---|
254 | time_point<steady_clock> startTime;
|
---|
255 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
256 |
|
---|
257 | int frameCount;
|
---|
258 |
|
---|
259 | /*** Functions ***/
|
---|
260 |
|
---|
261 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
262 | void initVulkan();
|
---|
263 | void initGraphicsPipelines();
|
---|
264 | void initMatrices();
|
---|
265 | void renderLoop();
|
---|
266 | void updateScene();
|
---|
267 | void cleanup();
|
---|
268 |
|
---|
269 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
270 | void setupDebugMessenger();
|
---|
271 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
272 | void createVulkanSurface();
|
---|
273 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
274 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
275 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
276 | const vector<const char*>& deviceExtensions);
|
---|
277 | void chooseSwapChainProperties();
|
---|
278 | void createSwapChain();
|
---|
279 | void createImageViews();
|
---|
280 | void createResourceCommandPool();
|
---|
281 | void createImageResources();
|
---|
282 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
283 | void createRenderPass();
|
---|
284 | void createCommandPools();
|
---|
285 | void createFramebuffers();
|
---|
286 | void createCommandBuffers();
|
---|
287 | void createSyncObjects();
|
---|
288 |
|
---|
289 | void createTextureSampler();
|
---|
290 |
|
---|
291 | void initImGuiOverlay();
|
---|
292 | void cleanupImGuiOverlay();
|
---|
293 |
|
---|
294 | // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
|
---|
295 |
|
---|
296 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags usages, VkMemoryPropertyFlags properties,
|
---|
297 | BufferSet& set);
|
---|
298 |
|
---|
299 | void resizeBufferSet(BufferSet& set, VkDeviceSize newSize, VkCommandPool commandPool, VkQueue graphicsQueue,
|
---|
300 | bool copyData);
|
---|
301 |
|
---|
302 | template<class SSBOType>
|
---|
303 | void updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo);
|
---|
304 |
|
---|
305 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
306 | // stop using objects.back() to access the object that was just created
|
---|
307 | template<class VertexType, class SSBOType>
|
---|
308 | SceneObject<VertexType>& addObject(vector<SceneObject<VertexType>>& objects,
|
---|
309 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
310 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
311 | VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo);
|
---|
312 |
|
---|
313 | template<class VertexType>
|
---|
314 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
315 |
|
---|
316 | template<class VertexType>
|
---|
317 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
318 |
|
---|
319 | template<class VertexType>
|
---|
320 | void centerObject(SceneObject<VertexType>& object);
|
---|
321 |
|
---|
322 | void renderFrame(ImDrawData* draw_data);
|
---|
323 | void presentFrame();
|
---|
324 |
|
---|
325 | void recreateSwapChain();
|
---|
326 |
|
---|
327 | void cleanupSwapChain();
|
---|
328 |
|
---|
329 | /*** High-level functions ***/
|
---|
330 |
|
---|
331 | void renderMainScreen(int width, int height);
|
---|
332 | void renderGameScreen(int width, int height);
|
---|
333 |
|
---|
334 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
335 | void renderGuiValueList(vector<UIValue>& values);
|
---|
336 |
|
---|
337 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
338 | void quitGame();
|
---|
339 | };
|
---|
340 |
|
---|
341 | // TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
|
---|
342 | template<class SSBOType>
|
---|
343 | void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
|
---|
344 | for (size_t i = 0; i < set.memory.size(); i++) {
|
---|
345 | VulkanUtils::copyDataToMemory(device, &ssbo, set.memory[i], objIndex * sizeof(SSBOType), sizeof(ssbo), false);
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model and to change
|
---|
350 | // the model matrix later by setting model_transform and then calculating the new ssbo.model.
|
---|
351 | // Figure out a better way to allow the model matrix to be set during object creation
|
---|
352 | template<class VertexType, class SSBOType>
|
---|
353 | SceneObject<VertexType>& VulkanGame::addObject(vector<SceneObject<VertexType>>& objects,
|
---|
354 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
355 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
356 | VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo) {
|
---|
357 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
358 | // currently, the passed-in model is useless since it gets overridden when ssbo.model is recalculated
|
---|
359 | size_t numVertices = pipeline.getNumVertices();
|
---|
360 |
|
---|
361 | for (uint16_t& idx : indices) {
|
---|
362 | idx += numVertices;
|
---|
363 | }
|
---|
364 |
|
---|
365 | objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
|
---|
366 | objectBuffer.add(ssbo);
|
---|
367 |
|
---|
368 | SceneObject<VertexType>& obj = objects.back();
|
---|
369 |
|
---|
370 | // TODO: Specify whether to center the object outside of this function or, worst case, maybe
|
---|
371 | // with a boolean being passed in here, so that I don't have to rely on checking the specific object
|
---|
372 | // type
|
---|
373 | // TODO: Actually, I've already defined a no-op centerObject method for explosions
|
---|
374 | // Maybe I should do the same for lasers and remove this conditional altogether
|
---|
375 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
376 | centerObject(obj);
|
---|
377 | }
|
---|
378 |
|
---|
379 | pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
|
---|
380 |
|
---|
381 | return obj;
|
---|
382 | }
|
---|
383 |
|
---|
384 | template<class VertexType>
|
---|
385 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
386 | for (VertexType& vertex : vertices) {
|
---|
387 | vertex.objIndex = objIndex;
|
---|
388 | }
|
---|
389 |
|
---|
390 | return vertices;
|
---|
391 | }
|
---|
392 |
|
---|
393 | template<class VertexType>
|
---|
394 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
395 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
396 | vec3 p1 = vertices[i].pos;
|
---|
397 | vec3 p2 = vertices[i + 1].pos;
|
---|
398 | vec3 p3 = vertices[i + 2].pos;
|
---|
399 |
|
---|
400 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
401 |
|
---|
402 | // Add the same normal for all 3 vertices
|
---|
403 | vertices[i].normal = normal;
|
---|
404 | vertices[i + 1].normal = normal;
|
---|
405 | vertices[i + 2].normal = normal;
|
---|
406 | }
|
---|
407 |
|
---|
408 | return vertices;
|
---|
409 | }
|
---|
410 |
|
---|
411 | template<class VertexType>
|
---|
412 | void VulkanGame::centerObject(SceneObject<VertexType>& object) {
|
---|
413 | vector<VertexType>& vertices = object.vertices;
|
---|
414 |
|
---|
415 | float min_x = vertices[0].pos.x;
|
---|
416 | float max_x = vertices[0].pos.x;
|
---|
417 | float min_y = vertices[0].pos.y;
|
---|
418 | float max_y = vertices[0].pos.y;
|
---|
419 | float min_z = vertices[0].pos.z;
|
---|
420 | float max_z = vertices[0].pos.z;
|
---|
421 |
|
---|
422 | // start from the second point
|
---|
423 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
424 | vec3& pos = vertices[i].pos;
|
---|
425 |
|
---|
426 | if (min_x > pos.x) {
|
---|
427 | min_x = pos.x;
|
---|
428 | }
|
---|
429 | else if (max_x < pos.x) {
|
---|
430 | max_x = pos.x;
|
---|
431 | }
|
---|
432 |
|
---|
433 | if (min_y > pos.y) {
|
---|
434 | min_y = pos.y;
|
---|
435 | }
|
---|
436 | else if (max_y < pos.y) {
|
---|
437 | max_y = pos.y;
|
---|
438 | }
|
---|
439 |
|
---|
440 | if (min_z > pos.z) {
|
---|
441 | min_z = pos.z;
|
---|
442 | }
|
---|
443 | else if (max_z < pos.z) {
|
---|
444 | max_z = pos.z;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
449 |
|
---|
450 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
451 | vertices[i].pos -= center;
|
---|
452 | }
|
---|
453 |
|
---|
454 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
455 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
456 |
|
---|
457 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
458 | }
|
---|
459 |
|
---|
460 | #endif // _SDL_GAME_H
|
---|