1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #define GLM_FORCE_RADIANS
|
---|
5 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
6 | #define GLM_FORCE_LEFT_HANDED
|
---|
7 |
|
---|
8 | #include <glm/glm.hpp>
|
---|
9 | #include <glm/gtc/matrix_transform.hpp>
|
---|
10 |
|
---|
11 | #include "game-gui-sdl.hpp"
|
---|
12 | #include "graphics-pipeline_vulkan.hpp"
|
---|
13 |
|
---|
14 | #include "vulkan-utils.hpp"
|
---|
15 |
|
---|
16 | using namespace glm;
|
---|
17 |
|
---|
18 | #ifdef NDEBUG
|
---|
19 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
20 | #else
|
---|
21 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | struct ModelVertex {
|
---|
25 | vec3 pos;
|
---|
26 | vec3 color;
|
---|
27 | vec2 texCoord;
|
---|
28 | };
|
---|
29 |
|
---|
30 | struct OverlayVertex {
|
---|
31 | vec3 pos;
|
---|
32 | vec2 texCoord;
|
---|
33 | };
|
---|
34 |
|
---|
35 | struct ShipVertex {
|
---|
36 | vec3 pos;
|
---|
37 | vec3 color;
|
---|
38 | vec3 normal;
|
---|
39 | unsigned int objIndex;
|
---|
40 | };
|
---|
41 |
|
---|
42 | struct UBO_VP_mats {
|
---|
43 | alignas(16) mat4 view;
|
---|
44 | alignas(16) mat4 proj;
|
---|
45 | };
|
---|
46 |
|
---|
47 | struct SBO_SceneObject {
|
---|
48 | alignas(16) mat4 model;
|
---|
49 | };
|
---|
50 |
|
---|
51 | class VulkanGame {
|
---|
52 | public:
|
---|
53 | VulkanGame(int maxFramesInFlight);
|
---|
54 | ~VulkanGame();
|
---|
55 |
|
---|
56 | void run(int width, int height, unsigned char guiFlags);
|
---|
57 |
|
---|
58 | private:
|
---|
59 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
60 |
|
---|
61 | const float NEAR_CLIP = 0.1f;
|
---|
62 | const float FAR_CLIP = 100.0f;
|
---|
63 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
64 |
|
---|
65 | vec3 cam_pos;
|
---|
66 |
|
---|
67 | UBO_VP_mats object_VP_mats;
|
---|
68 | SBO_SceneObject so_Object;
|
---|
69 |
|
---|
70 | UBO_VP_mats ship_VP_mats;
|
---|
71 | SBO_SceneObject so_Ship;
|
---|
72 |
|
---|
73 | GameGui* gui;
|
---|
74 |
|
---|
75 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
76 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
---|
77 | GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
|
---|
78 |
|
---|
79 | SDL_version sdlVersion;
|
---|
80 | SDL_Window* window = nullptr;
|
---|
81 | SDL_Renderer* renderer = nullptr;
|
---|
82 |
|
---|
83 | SDL_Texture* uiOverlay = nullptr;
|
---|
84 |
|
---|
85 | VkInstance instance;
|
---|
86 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
87 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
88 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
89 | VkDevice device;
|
---|
90 |
|
---|
91 | VkQueue graphicsQueue;
|
---|
92 | VkQueue presentQueue;
|
---|
93 |
|
---|
94 | VkSwapchainKHR swapChain;
|
---|
95 | vector<VkImage> swapChainImages;
|
---|
96 | VkFormat swapChainImageFormat;
|
---|
97 | VkExtent2D swapChainExtent;
|
---|
98 | vector<VkImageView> swapChainImageViews;
|
---|
99 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
100 |
|
---|
101 | VkRenderPass renderPass;
|
---|
102 | VkCommandPool commandPool;
|
---|
103 | vector<VkCommandBuffer> commandBuffers;
|
---|
104 |
|
---|
105 | VulkanImage depthImage;
|
---|
106 |
|
---|
107 | VkSampler textureSampler;
|
---|
108 |
|
---|
109 | // TODO: I should probably rename the uniformBuffer* and storageBuffer*
|
---|
110 | // variables to better reflect the data they hold
|
---|
111 |
|
---|
112 | vector<VkBuffer> uniformBuffers_scenePipeline;
|
---|
113 | vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
|
---|
114 |
|
---|
115 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
|
---|
116 |
|
---|
117 | vector<VkBuffer> storageBuffers_scenePipeline;
|
---|
118 | vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
|
---|
119 |
|
---|
120 | vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
|
---|
121 |
|
---|
122 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
123 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
124 |
|
---|
125 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
126 |
|
---|
127 | vector<VkBuffer> storageBuffers_shipPipeline;
|
---|
128 | vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
|
---|
129 |
|
---|
130 | vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
|
---|
131 |
|
---|
132 | VulkanImage floorTextureImage;
|
---|
133 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
134 |
|
---|
135 | VulkanImage sdlOverlayImage;
|
---|
136 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
137 |
|
---|
138 | TTF_Font* font;
|
---|
139 | SDL_Texture* fontSDLTexture;
|
---|
140 |
|
---|
141 | SDL_Texture* imageSDLTexture;
|
---|
142 |
|
---|
143 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
144 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
145 | vector<VkFence> inFlightFences;
|
---|
146 |
|
---|
147 | size_t currentFrame;
|
---|
148 |
|
---|
149 | bool framebufferResized;
|
---|
150 |
|
---|
151 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
152 | void initVulkan();
|
---|
153 | void initGraphicsPipelines();
|
---|
154 | void initMatrices();
|
---|
155 | void mainLoop();
|
---|
156 | void updateScene(uint32_t currentImage);
|
---|
157 | void renderUI();
|
---|
158 | void renderScene();
|
---|
159 | void cleanup();
|
---|
160 |
|
---|
161 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
162 | void setupDebugMessenger();
|
---|
163 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
164 | void createVulkanSurface();
|
---|
165 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
166 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
167 | void createLogicalDevice(
|
---|
168 | const vector<const char*> validationLayers,
|
---|
169 | const vector<const char*>& deviceExtensions);
|
---|
170 | void createSwapChain();
|
---|
171 | void createImageViews();
|
---|
172 | void createRenderPass();
|
---|
173 | VkFormat findDepthFormat();
|
---|
174 | void createCommandPool();
|
---|
175 | void createImageResources();
|
---|
176 |
|
---|
177 | void createTextureSampler();
|
---|
178 | void createFramebuffers();
|
---|
179 | void createCommandBuffers();
|
---|
180 | void createSyncObjects();
|
---|
181 |
|
---|
182 | template<class VertexType>
|
---|
183 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
184 |
|
---|
185 | template<class VertexType>
|
---|
186 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
187 |
|
---|
188 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
189 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
190 |
|
---|
191 | void recreateSwapChain();
|
---|
192 |
|
---|
193 | void cleanupSwapChain();
|
---|
194 |
|
---|
195 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
196 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
197 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
198 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
199 | void* pUserData);
|
---|
200 | };
|
---|
201 |
|
---|
202 | template<class VertexType>
|
---|
203 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
204 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
205 | vec3 p1 = vertices[i].pos;
|
---|
206 | vec3 p2 = vertices[i+1].pos;
|
---|
207 | vec3 p3 = vertices[i+2].pos;
|
---|
208 |
|
---|
209 | // flip the y axis so that +y points up
|
---|
210 | vec3 normal = -normalize(cross(p2 - p1, p3 - p1));
|
---|
211 |
|
---|
212 | // Add the same normal for all 3 vertices
|
---|
213 | vertices[i].normal = normal;
|
---|
214 | vertices[i+1].normal = normal;
|
---|
215 | vertices[i+2].normal = normal;
|
---|
216 | }
|
---|
217 |
|
---|
218 | return vertices;
|
---|
219 | }
|
---|
220 |
|
---|
221 | template<class VertexType>
|
---|
222 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
223 | for (VertexType& vertex : vertices) {
|
---|
224 | vertex.objIndex = objIndex;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return vertices;
|
---|
228 | }
|
---|
229 |
|
---|
230 | #endif // _VULKAN_GAME_H
|
---|