source: opengl-game/vulkan-game.hpp@ 2da64ef

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

In VulkanGame, move the logic of updating per-object data in the SSBO into GraphicsPipeline_Vulkan and remove the SSBO properties from VulkanGame

  • 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
77// Have to figure out how to include an optional ssbo parameter for each object
78// Could probably use the same approach to make indices optional
79
80class VulkanGame {
81 public:
82 VulkanGame(int maxFramesInFlight);
83 ~VulkanGame();
84
85 void run(int width, int height, unsigned char guiFlags);
86
87 private:
88 const int MAX_FRAMES_IN_FLIGHT;
89
90 const float NEAR_CLIP = 0.1f;
91 const float FAR_CLIP = 100.0f;
92 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
93
94 vec3 cam_pos;
95
96 GameGui* gui;
97
98 SDL_version sdlVersion;
99 SDL_Window* window = nullptr;
100 SDL_Renderer* renderer = nullptr;
101
102 SDL_Texture* uiOverlay = nullptr;
103
104 VkInstance instance;
105 VkDebugUtilsMessengerEXT debugMessenger;
106 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
107 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
108 VkDevice device;
109
110 VkQueue graphicsQueue;
111 VkQueue presentQueue;
112
113 VkSwapchainKHR swapChain;
114 vector<VkImage> swapChainImages;
115 VkFormat swapChainImageFormat;
116 VkExtent2D swapChainExtent;
117 vector<VkImageView> swapChainImageViews;
118 vector<VkFramebuffer> swapChainFramebuffers;
119
120 VkRenderPass renderPass;
121 VkCommandPool commandPool;
122 vector<VkCommandBuffer> commandBuffers;
123
124 VulkanImage depthImage;
125
126 VkSampler textureSampler;
127
128 VulkanImage floorTextureImage;
129 VkDescriptorImageInfo floorTextureImageDescriptor;
130
131 VulkanImage sdlOverlayImage;
132 VkDescriptorImageInfo sdlOverlayImageDescriptor;
133
134 TTF_Font* font;
135 SDL_Texture* fontSDLTexture;
136
137 SDL_Texture* imageSDLTexture;
138
139 vector<VkSemaphore> imageAvailableSemaphores;
140 vector<VkSemaphore> renderFinishedSemaphores;
141 vector<VkFence> inFlightFences;
142
143 size_t currentFrame;
144
145 bool framebufferResized;
146
147 GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
148 vector<SceneObject<OverlayVertex, void*>> overlayObjects;
149
150 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
151 // per pipeline.
152 // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
153 // the objects vector, the ubo, and the ssbo
154
155 GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
156 vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
157
158 vector<VkBuffer> uniformBuffers_modelPipeline;
159 vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
160 vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
161
162 UBO_VP_mats object_VP_mats;
163
164 GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
165 vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
166
167 vector<VkBuffer> uniformBuffers_shipPipeline;
168 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
169 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
170
171 UBO_VP_mats ship_VP_mats;
172
173 GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
174 vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
175
176 vector<VkBuffer> uniformBuffers_asteroidPipeline;
177 vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
178 vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
179
180 UBO_VP_mats asteroid_VP_mats;
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, class SSBOType>
222 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
223 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
224
225 template<class VertexType>
226 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
227
228 template<class VertexType>
229 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
230
231 template<class VertexType>
232 vector<VertexType> centerObject(vector<VertexType> vertices);
233
234 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
235 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
236
237 void recreateSwapChain();
238
239 void cleanupSwapChain();
240
241 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
242 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
243 VkDebugUtilsMessageTypeFlagsEXT messageType,
244 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
245 void* pUserData);
246};
247
248template<class VertexType, class SSBOType>
249void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
250 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
251 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo) {
252 size_t numVertices = pipeline.getNumVertices();
253
254 for (uint16_t& idx : indices) {
255 idx += numVertices;
256 }
257
258 objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
259
260 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
261}
262
263template<class VertexType, class SSBOType>
264void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
265 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
266 SceneObject<VertexType, SSBOType>& obj = objects[index];
267
268 obj.ssbo.model = obj.model_transform * obj.model_base;
269
270 // could probably re-calculate the object center here based on model
271 // I think the center should be calculated by using model * vec3(0, 0, 0)
272 // model_base is currently only used to set the original location of the ship and asteroids
273
274 pipeline.updateObject(index, obj.ssbo);
275}
276
277template<class VertexType>
278vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
279 for (unsigned int i = 0; i < vertices.size(); i += 3) {
280 vec3 p1 = vertices[i].pos;
281 vec3 p2 = vertices[i+1].pos;
282 vec3 p3 = vertices[i+2].pos;
283
284 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
285
286 // Add the same normal for all 3 vertices
287 vertices[i].normal = normal;
288 vertices[i+1].normal = normal;
289 vertices[i+2].normal = normal;
290 }
291
292 return vertices;
293}
294
295template<class VertexType>
296vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
297 for (VertexType& vertex : vertices) {
298 vertex.objIndex = objIndex;
299 }
300
301 return vertices;
302}
303
304template<class VertexType>
305vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
306 float min_x = vertices[0].pos.x;
307 float max_x = vertices[0].pos.x;
308 float min_y = vertices[0].pos.y;
309 float max_y = vertices[0].pos.y;
310 float min_z = vertices[0].pos.z;
311 float max_z = vertices[0].pos.z;
312
313 // start from the second point
314 for (unsigned int i = 1; i < vertices.size(); i++) {
315 if (min_x > vertices[i].pos.x) {
316 min_x = vertices[i].pos.x;
317 } else if (max_x < vertices[i].pos.x) {
318 max_x = vertices[i].pos.x;
319 }
320
321 if (min_y > vertices[i].pos.y) {
322 min_y = vertices[i].pos.y;
323 } else if (max_y < vertices[i].pos.y) {
324 max_y = vertices[i].pos.y;
325 }
326
327 if (min_z > vertices[i].pos.z) {
328 min_z = vertices[i].pos.z;
329 } else if (max_z < vertices[i].pos.z) {
330 max_z = vertices[i].pos.z;
331 }
332 }
333
334 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
335
336 for (unsigned int i = 0; i < vertices.size(); i++) {
337 vertices[i].pos -= center;
338 }
339
340 return vertices;
341}
342
343#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.