source: opengl-game/vulkan-game.hpp@ 3b84bb6

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

In VulkanGame, call centerObject() on all objects when they are created and move that call inside VulkanGame::addObject(), rename GraphicsPipeline_Vulkan::addVertices() to addObject, add start adding generic support for objects to be added after pipeline creation is complete

  • Property mode set to 100644
File size: 11.0 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 vec3 center;
61 float radius;
62};
63
64struct UBO_VP_mats {
65 alignas(16) mat4 view;
66 alignas(16) mat4 proj;
67};
68
69struct SSBO_ModelObject {
70 alignas(16) mat4 model;
71};
72
73struct SSBO_Asteroid {
74 alignas(16) mat4 model;
75 alignas(4) float hp;
76 alignas(4) unsigned int deleted;
77};
78
79// Have to figure out how to include an optional ssbo parameter for each object
80// Could probably use the same approach to make indices optional
81
82class VulkanGame {
83 public:
84 VulkanGame(int maxFramesInFlight);
85 ~VulkanGame();
86
87 void run(int width, int height, unsigned char guiFlags);
88
89 private:
90 const int MAX_FRAMES_IN_FLIGHT;
91
92 const float NEAR_CLIP = 0.1f;
93 const float FAR_CLIP = 100.0f;
94 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
95
96 vec3 cam_pos;
97
98 GameGui* gui;
99
100 SDL_version sdlVersion;
101 SDL_Window* window = nullptr;
102 SDL_Renderer* renderer = nullptr;
103
104 SDL_Texture* uiOverlay = nullptr;
105
106 VkInstance instance;
107 VkDebugUtilsMessengerEXT debugMessenger;
108 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
109 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
110 VkDevice device;
111
112 VkQueue graphicsQueue;
113 VkQueue presentQueue;
114
115 VkSwapchainKHR swapChain;
116 vector<VkImage> swapChainImages;
117 VkFormat swapChainImageFormat;
118 VkExtent2D swapChainExtent;
119 vector<VkImageView> swapChainImageViews;
120 vector<VkFramebuffer> swapChainFramebuffers;
121
122 VkRenderPass renderPass;
123 VkCommandPool commandPool;
124 vector<VkCommandBuffer> commandBuffers;
125
126 VulkanImage depthImage;
127
128 VkSampler textureSampler;
129
130 VulkanImage floorTextureImage;
131 VkDescriptorImageInfo floorTextureImageDescriptor;
132
133 VulkanImage sdlOverlayImage;
134 VkDescriptorImageInfo sdlOverlayImageDescriptor;
135
136 TTF_Font* font;
137 SDL_Texture* fontSDLTexture;
138
139 SDL_Texture* imageSDLTexture;
140
141 vector<VkSemaphore> imageAvailableSemaphores;
142 vector<VkSemaphore> renderFinishedSemaphores;
143 vector<VkFence> inFlightFences;
144
145 size_t currentFrame;
146
147 bool framebufferResized;
148
149 GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
150 vector<SceneObject<OverlayVertex, void*>> overlayObjects;
151
152 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
153 // per pipeline.
154 // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
155 // the objects vector, the ubo, and the ssbo
156
157 GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
158 vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
159
160 vector<VkBuffer> uniformBuffers_modelPipeline;
161 vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
162 vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
163
164 UBO_VP_mats object_VP_mats;
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 UBO_VP_mats ship_VP_mats;
174
175 GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
176 vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
177
178 vector<VkBuffer> uniformBuffers_asteroidPipeline;
179 vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
180 vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
181
182 UBO_VP_mats asteroid_VP_mats;
183
184 Uint64 curTime, prevTime;
185 double elapsedTime;
186
187 bool initWindow(int width, int height, unsigned char guiFlags);
188 void initVulkan();
189 void initGraphicsPipelines();
190 void initMatrices();
191 void mainLoop();
192 void updateScene(uint32_t currentImage);
193 void renderUI();
194 void renderScene();
195 void cleanup();
196
197 void createVulkanInstance(const vector<const char*> &validationLayers);
198 void setupDebugMessenger();
199 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
200 void createVulkanSurface();
201 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
202 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
203 void createLogicalDevice(
204 const vector<const char*> validationLayers,
205 const vector<const char*>& deviceExtensions);
206 void createSwapChain();
207 void createImageViews();
208 void createRenderPass();
209 VkFormat findDepthFormat();
210 void createCommandPool();
211 void createImageResources();
212
213 void createTextureSampler();
214 void createFramebuffers();
215 void createCommandBuffers();
216 void createSyncObjects();
217
218 template<class VertexType, class SSBOType>
219 void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
220 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
221 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
222 bool pipelinesCreated);
223
224 template<class VertexType, class SSBOType>
225 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
226 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
227
228 template<class VertexType>
229 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
230
231 template<class VertexType>
232 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
233
234 template<class VertexType, class SSBOType>
235 void centerObject(SceneObject<VertexType, SSBOType>& object);
236
237 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
238 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
239
240 void recreateSwapChain();
241
242 void cleanupSwapChain();
243
244 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
245 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
246 VkDebugUtilsMessageTypeFlagsEXT messageType,
247 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
248 void* pUserData);
249};
250
251// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
252// and to change the model matrix later by setting model_transform and then calling updateObject()
253// Figure out a better way to allow the model matrix to be set during objecting creation
254template<class VertexType, class SSBOType>
255void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
256 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
257 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
258 bool pipelinesCreated) {
259 size_t numVertices = pipeline.getNumVertices();
260
261 for (uint16_t& idx : indices) {
262 idx += numVertices;
263 }
264
265 objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
266 centerObject(objects.back());
267
268 bool storageBufferResized = pipeline.addObject(vertices, indices, ssbo, commandPool, graphicsQueue);
269
270 if (pipelinesCreated) {
271 if (storageBufferResized) {
272 }
273
274 createCommandBuffers();
275 }
276}
277
278template<class VertexType, class SSBOType>
279void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
280 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
281 SceneObject<VertexType, SSBOType>& obj = objects[index];
282
283 obj.ssbo.model = obj.model_transform * obj.model_base;
284
285 // could probably re-calculate the object center here based on model
286 // I think the center should be calculated by using model * vec3(0, 0, 0)
287 // model_base is currently only used to set the original location of the ship and asteroids
288
289 pipeline.updateObject(index, obj.ssbo);
290}
291
292template<class VertexType>
293vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
294 for (unsigned int i = 0; i < vertices.size(); i += 3) {
295 vec3 p1 = vertices[i].pos;
296 vec3 p2 = vertices[i+1].pos;
297 vec3 p3 = vertices[i+2].pos;
298
299 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
300
301 // Add the same normal for all 3 vertices
302 vertices[i].normal = normal;
303 vertices[i+1].normal = normal;
304 vertices[i+2].normal = normal;
305 }
306
307 return vertices;
308}
309
310template<class VertexType>
311vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
312 for (VertexType& vertex : vertices) {
313 vertex.objIndex = objIndex;
314 }
315
316 return vertices;
317}
318
319template<class VertexType, class SSBOType>
320void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
321 vector<VertexType>& vertices = object.vertices;
322
323 float min_x = vertices[0].pos.x;
324 float max_x = vertices[0].pos.x;
325 float min_y = vertices[0].pos.y;
326 float max_y = vertices[0].pos.y;
327 float min_z = vertices[0].pos.z;
328 float max_z = vertices[0].pos.z;
329
330 // start from the second point
331 for (unsigned int i = 1; i < vertices.size(); i++) {
332 vec3& pos = vertices[i].pos;
333
334 if (min_x > pos.x) {
335 min_x = pos.x;
336 } else if (max_x < pos.x) {
337 max_x = pos.x;
338 }
339
340 if (min_y > pos.y) {
341 min_y = pos.y;
342 } else if (max_y < pos.y) {
343 max_y = pos.y;
344 }
345
346 if (min_z > pos.z) {
347 min_z = pos.z;
348 } else if (max_z < pos.z) {
349 max_z = pos.z;
350 }
351 }
352
353 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
354
355 for (unsigned int i = 0; i < vertices.size(); i++) {
356 vertices[i].pos -= center;
357 }
358
359 object.radius = std::max(center.x, center.y);
360 object.radius = std::max(object.radius, center.z);
361 object.center = vec3(0.0f, 0.0f, 0.0f);
362}
363
364#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.