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