source: opengl-game/vulkan-game.hpp@ 4ece3bf

feature/imgui-sdl points-test
Last change on this file since 4ece3bf was 4ece3bf, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In VulkanGame, add the ability to delete asteroids and move them at a constant speed

  • Property mode set to 100644
File size: 10.1 KB
Line 
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_RIGHT_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
16using 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
24struct ModelVertex {
25 vec3 pos;
26 vec3 color;
27 vec2 texCoord;
28};
29
30struct OverlayVertex {
31 vec3 pos;
32 vec2 texCoord;
33};
34
35struct ShipVertex {
36 vec3 pos;
37 vec3 color;
38 vec3 normal;
39 unsigned int objIndex;
40};
41
42struct AsteroidVertex {
43 vec3 pos;
44 vec3 color;
45 vec3 normal;
46 unsigned int objIndex;
47};
48
49// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
50// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
51template<class VertexType>
52struct SceneObject {
53 vector<VertexType> vertices;
54 vector<uint16_t> indices;
55
56 mat4 model_base;
57 mat4 model_transform;
58};
59
60struct UBO_VP_mats {
61 alignas(16) mat4 view;
62 alignas(16) mat4 proj;
63};
64
65struct SBO_SceneObject {
66 alignas(16) mat4 model;
67};
68
69struct SBO_Asteroid {
70 alignas(16) mat4 model;
71 alignas(4) float hp;
72 alignas(4) unsigned int deleted;
73};
74
75class VulkanGame {
76 public:
77 VulkanGame(int maxFramesInFlight);
78 ~VulkanGame();
79
80 void run(int width, int height, unsigned char guiFlags);
81
82 private:
83 const int MAX_FRAMES_IN_FLIGHT;
84
85 const float NEAR_CLIP = 0.1f;
86 const float FAR_CLIP = 100.0f;
87 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
88
89 vec3 cam_pos;
90
91 GameGui* gui;
92
93 SDL_version sdlVersion;
94 SDL_Window* window = nullptr;
95 SDL_Renderer* renderer = nullptr;
96
97 SDL_Texture* uiOverlay = nullptr;
98
99 VkInstance instance;
100 VkDebugUtilsMessengerEXT debugMessenger;
101 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
102 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
103 VkDevice device;
104
105 VkQueue graphicsQueue;
106 VkQueue presentQueue;
107
108 VkSwapchainKHR swapChain;
109 vector<VkImage> swapChainImages;
110 VkFormat swapChainImageFormat;
111 VkExtent2D swapChainExtent;
112 vector<VkImageView> swapChainImageViews;
113 vector<VkFramebuffer> swapChainFramebuffers;
114
115 VkRenderPass renderPass;
116 VkCommandPool commandPool;
117 vector<VkCommandBuffer> commandBuffers;
118
119 VulkanImage depthImage;
120
121 VkSampler textureSampler;
122
123 VulkanImage floorTextureImage;
124 VkDescriptorImageInfo floorTextureImageDescriptor;
125
126 VulkanImage sdlOverlayImage;
127 VkDescriptorImageInfo sdlOverlayImageDescriptor;
128
129 TTF_Font* font;
130 SDL_Texture* fontSDLTexture;
131
132 SDL_Texture* imageSDLTexture;
133
134 vector<VkSemaphore> imageAvailableSemaphores;
135 vector<VkSemaphore> renderFinishedSemaphores;
136 vector<VkFence> inFlightFences;
137
138 size_t currentFrame;
139
140 bool framebufferResized;
141
142 // TODO: I should probably rename the uniformBuffer* and storageBuffer*
143 // variables to better reflect the data they hold
144
145 // TODO: Create a struct that holds the buffers, memory, and info objects (Probably in VulkanUtils)
146
147 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
148
149 vector<SceneObject<OverlayVertex>> overlayObjects;
150
151 // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
152
153 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
154
155 vector<SceneObject<ModelVertex>> modelObjects;
156
157 vector<VkBuffer> uniformBuffers_scenePipeline;
158 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
159 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
160
161 vector<VkBuffer> storageBuffers_scenePipeline;
162 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
163 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
164
165 UBO_VP_mats object_VP_mats;
166 SBO_SceneObject so_Object;
167
168 GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
169
170 vector<SceneObject<ShipVertex>> shipObjects;
171
172 vector<VkBuffer> uniformBuffers_shipPipeline;
173 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
174 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
175
176 vector<VkBuffer> storageBuffers_shipPipeline;
177 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
178 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
179
180 UBO_VP_mats ship_VP_mats;
181 SBO_SceneObject so_Ship;
182
183 GraphicsPipeline_Vulkan<AsteroidVertex> asteroidPipeline;
184
185 vector<SceneObject<AsteroidVertex>> asteroidObjects;
186
187 vector<VkBuffer> uniformBuffers_asteroidPipeline;
188 vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
189 vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
190
191 vector<VkBuffer> storageBuffers_asteroidPipeline;
192 vector<VkDeviceMemory> storageBuffersMemory_asteroidPipeline;
193 vector<VkDescriptorBufferInfo> storageBufferInfoList_asteroidPipeline;
194
195 UBO_VP_mats asteroid_VP_mats;
196 SBO_Asteroid so_Asteroid;
197
198 Uint64 curTime, prevTime;
199 double elapsedTime;
200
201 bool initWindow(int width, int height, unsigned char guiFlags);
202 void initVulkan();
203 void initGraphicsPipelines();
204 void initMatrices();
205 void mainLoop();
206 void updateScene(uint32_t currentImage);
207 void renderUI();
208 void renderScene();
209 void cleanup();
210
211 void createVulkanInstance(const vector<const char*> &validationLayers);
212 void setupDebugMessenger();
213 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
214 void createVulkanSurface();
215 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
216 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
217 void createLogicalDevice(
218 const vector<const char*> validationLayers,
219 const vector<const char*>& deviceExtensions);
220 void createSwapChain();
221 void createImageViews();
222 void createRenderPass();
223 VkFormat findDepthFormat();
224 void createCommandPool();
225 void createImageResources();
226
227 void createTextureSampler();
228 void createFramebuffers();
229 void createCommandBuffers();
230 void createSyncObjects();
231
232 template<class VertexType>
233 void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
234 const vector<VertexType>& vertices, vector<uint16_t> indices);
235
236 template<class VertexType>
237 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
238
239 template<class VertexType>
240 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
241
242 template<class VertexType>
243 vector<VertexType> centerObject(vector<VertexType> vertices);
244
245 template<class VertexType>
246 void transformObject(SceneObject<VertexType>& obj, mat4 mat);
247
248 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
249 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
250
251 void recreateSwapChain();
252
253 void cleanupSwapChain();
254
255 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
256 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
257 VkDebugUtilsMessageTypeFlagsEXT messageType,
258 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
259 void* pUserData);
260};
261
262template<class VertexType>
263void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
264 const vector<VertexType>& vertices, vector<uint16_t> indices) {
265 size_t numVertices = pipeline.getNumVertices();
266
267 for (uint16_t& idx : indices) {
268 idx += numVertices;
269 }
270
271 objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
272
273 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
274}
275
276template<class VertexType>
277vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
278 for (unsigned int i = 0; i < vertices.size(); i += 3) {
279 vec3 p1 = vertices[i].pos;
280 vec3 p2 = vertices[i+1].pos;
281 vec3 p3 = vertices[i+2].pos;
282
283 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
284
285 // Add the same normal for all 3 vertices
286 vertices[i].normal = normal;
287 vertices[i+1].normal = normal;
288 vertices[i+2].normal = normal;
289 }
290
291 return vertices;
292}
293
294template<class VertexType>
295vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
296 for (VertexType& vertex : vertices) {
297 vertex.objIndex = objIndex;
298 }
299
300 return vertices;
301}
302
303template<class VertexType>
304vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
305 float min_x = vertices[0].pos.x;
306 float max_x = vertices[0].pos.x;
307 float min_y = vertices[0].pos.y;
308 float max_y = vertices[0].pos.y;
309 float min_z = vertices[0].pos.z;
310 float max_z = vertices[0].pos.z;
311
312 // start from the second point
313 for (unsigned int i = 1; i < vertices.size(); i++) {
314 if (min_x > vertices[i].pos.x) {
315 min_x = vertices[i].pos.x;
316 } else if (max_x < vertices[i].pos.x) {
317 max_x = vertices[i].pos.x;
318 }
319
320 if (min_y > vertices[i].pos.y) {
321 min_y = vertices[i].pos.y;
322 } else if (max_y < vertices[i].pos.y) {
323 max_y = vertices[i].pos.y;
324 }
325
326 if (min_z > vertices[i].pos.z) {
327 min_z = vertices[i].pos.z;
328 } else if (max_z < vertices[i].pos.z) {
329 max_z = vertices[i].pos.z;
330 }
331 }
332
333 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
334
335 for (unsigned int i = 0; i < vertices.size(); i++) {
336 vertices[i].pos -= center;
337 }
338
339 return vertices;
340}
341
342template<class VertexType>
343void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
344 obj.model_transform = mat * obj.model_transform;
345}
346
347#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.