source: opengl-game/vulkan-game.hpp@ 860a0da

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

In VulkanGame, move fields related to the ssbo, as well as code to create thee ssbo, destroy it, and create a descriptor for it, into GraphicsPipeline_Vulkan

  • Property mode set to 100644
File size: 9.8 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 GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
145 vector<SceneObject<OverlayVertex, void*>> overlayObjects;
146
147 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
148 // per pipeline.
149 // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
150 // the objects vector, the ubo, and the ssbo
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 UBO_VP_mats object_VP_mats;
160 SSBO_ModelObject so_Object;
161
162 GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
163 vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
164
165 vector<VkBuffer> uniformBuffers_shipPipeline;
166 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
167 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
168
169 UBO_VP_mats ship_VP_mats;
170 SSBO_ModelObject so_Ship;
171
172 GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
173 vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
174
175 vector<VkBuffer> uniformBuffers_asteroidPipeline;
176 vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
177 vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
178
179 UBO_VP_mats asteroid_VP_mats;
180 SSBO_Asteroid so_Asteroid;
181
182 Uint64 curTime, prevTime;
183 double elapsedTime;
184
185 bool initWindow(int width, int height, unsigned char guiFlags);
186 void initVulkan();
187 void initGraphicsPipelines();
188 void initMatrices();
189 void mainLoop();
190 void updateScene(uint32_t currentImage);
191 void renderUI();
192 void renderScene();
193 void cleanup();
194
195 void createVulkanInstance(const vector<const char*> &validationLayers);
196 void setupDebugMessenger();
197 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
198 void createVulkanSurface();
199 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
200 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
201 void createLogicalDevice(
202 const vector<const char*> validationLayers,
203 const vector<const char*>& deviceExtensions);
204 void createSwapChain();
205 void createImageViews();
206 void createRenderPass();
207 VkFormat findDepthFormat();
208 void createCommandPool();
209 void createImageResources();
210
211 void createTextureSampler();
212 void createFramebuffers();
213 void createCommandBuffers();
214 void createSyncObjects();
215
216 template<class VertexType, class SSBOType>
217 void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
218 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
219 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo);
220
221 template<class VertexType>
222 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
223
224 template<class VertexType>
225 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
226
227 template<class VertexType>
228 vector<VertexType> centerObject(vector<VertexType> vertices);
229
230 template<class VertexType, class SSBOType>
231 void transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat);
232
233 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
234 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
235
236 void recreateSwapChain();
237
238 void cleanupSwapChain();
239
240 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
241 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
242 VkDebugUtilsMessageTypeFlagsEXT messageType,
243 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
244 void* pUserData);
245};
246
247template<class VertexType, class SSBOType>
248void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
249 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
250 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo) {
251 size_t numVertices = pipeline.getNumVertices();
252
253 for (uint16_t& idx : indices) {
254 idx += numVertices;
255 }
256
257 objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
258
259 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
260}
261
262template<class VertexType>
263vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
264 for (unsigned int i = 0; i < vertices.size(); i += 3) {
265 vec3 p1 = vertices[i].pos;
266 vec3 p2 = vertices[i+1].pos;
267 vec3 p3 = vertices[i+2].pos;
268
269 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
270
271 // Add the same normal for all 3 vertices
272 vertices[i].normal = normal;
273 vertices[i+1].normal = normal;
274 vertices[i+2].normal = normal;
275 }
276
277 return vertices;
278}
279
280template<class VertexType>
281vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
282 for (VertexType& vertex : vertices) {
283 vertex.objIndex = objIndex;
284 }
285
286 return vertices;
287}
288
289template<class VertexType>
290vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
291 float min_x = vertices[0].pos.x;
292 float max_x = vertices[0].pos.x;
293 float min_y = vertices[0].pos.y;
294 float max_y = vertices[0].pos.y;
295 float min_z = vertices[0].pos.z;
296 float max_z = vertices[0].pos.z;
297
298 // start from the second point
299 for (unsigned int i = 1; i < vertices.size(); i++) {
300 if (min_x > vertices[i].pos.x) {
301 min_x = vertices[i].pos.x;
302 } else if (max_x < vertices[i].pos.x) {
303 max_x = vertices[i].pos.x;
304 }
305
306 if (min_y > vertices[i].pos.y) {
307 min_y = vertices[i].pos.y;
308 } else if (max_y < vertices[i].pos.y) {
309 max_y = vertices[i].pos.y;
310 }
311
312 if (min_z > vertices[i].pos.z) {
313 min_z = vertices[i].pos.z;
314 } else if (max_z < vertices[i].pos.z) {
315 max_z = vertices[i].pos.z;
316 }
317 }
318
319 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
320
321 for (unsigned int i = 0; i < vertices.size(); i++) {
322 vertices[i].pos -= center;
323 }
324
325 return vertices;
326}
327
328template<class VertexType, class SSBOType>
329void VulkanGame::transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat) {
330 obj.model_transform = mat * obj.model_transform;
331}
332
333#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.