source: opengl-game/vulkan-game.hpp@ d25381b

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

In VulkanGame, rename fields related to the ubo and ssbo for the model pipeline to end in _modelPipeline rather than scenePipeline

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