1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #include <algorithm>
|
---|
5 | #include <chrono>
|
---|
6 | #include <map>
|
---|
7 | #include <vector>
|
---|
8 |
|
---|
9 | #include <vulkan/vulkan.h>
|
---|
10 |
|
---|
11 | #include <SDL2/SDL.h>
|
---|
12 | #include <SDL2/SDL_ttf.h>
|
---|
13 |
|
---|
14 | #define GLM_FORCE_RADIANS
|
---|
15 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
16 | #define GLM_FORCE_RIGHT_HANDED
|
---|
17 |
|
---|
18 | #include <glm/glm.hpp>
|
---|
19 | #include <glm/gtc/matrix_transform.hpp>
|
---|
20 |
|
---|
21 | #include "IMGUI/imgui_impl_vulkan.h"
|
---|
22 |
|
---|
23 | #include "consts.hpp"
|
---|
24 | #include "utils.hpp"
|
---|
25 | #include "vulkan-utils.hpp"
|
---|
26 | #include "vulkan-buffer.hpp"
|
---|
27 | #include "graphics-pipeline_vulkan.hpp"
|
---|
28 | #include "game-gui-sdl.hpp"
|
---|
29 |
|
---|
30 | using namespace glm;
|
---|
31 | using namespace std::chrono;
|
---|
32 |
|
---|
33 | #ifdef NDEBUG
|
---|
34 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
35 | #else
|
---|
36 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | // TODO: Consider if there is a better way of dealing with all the vertex types and ssbo types, maybe
|
---|
40 | // by consolidating some and trying to keep new ones to a minimum
|
---|
41 |
|
---|
42 | struct OverlayVertex {
|
---|
43 | vec3 pos;
|
---|
44 | vec2 texCoord;
|
---|
45 | };
|
---|
46 |
|
---|
47 | struct ModelVertex {
|
---|
48 | vec3 pos;
|
---|
49 | vec3 color;
|
---|
50 | vec2 texCoord;
|
---|
51 | vec3 normal;
|
---|
52 | unsigned int objIndex;
|
---|
53 | };
|
---|
54 |
|
---|
55 | struct LaserVertex {
|
---|
56 | vec3 pos;
|
---|
57 | vec2 texCoord;
|
---|
58 | unsigned int objIndex;
|
---|
59 | };
|
---|
60 |
|
---|
61 | struct ExplosionVertex {
|
---|
62 | vec3 particleStartVelocity;
|
---|
63 | float particleStartTime;
|
---|
64 | unsigned int objIndex;
|
---|
65 | };
|
---|
66 |
|
---|
67 | struct SSBO_ModelObject {
|
---|
68 | alignas(16) mat4 model;
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct SSBO_Asteroid {
|
---|
72 | alignas(16) mat4 model;
|
---|
73 | alignas(4) float hp;
|
---|
74 | alignas(4) unsigned int deleted;
|
---|
75 | };
|
---|
76 |
|
---|
77 | struct SSBO_Laser {
|
---|
78 | alignas(16) mat4 model;
|
---|
79 | alignas(4) vec3 color;
|
---|
80 | alignas(4) unsigned int deleted;
|
---|
81 | };
|
---|
82 |
|
---|
83 | struct SSBO_Explosion {
|
---|
84 | alignas(16) mat4 model;
|
---|
85 | alignas(4) float explosionStartTime;
|
---|
86 | alignas(4) float explosionDuration;
|
---|
87 | alignas(4) unsigned int deleted;
|
---|
88 | };
|
---|
89 |
|
---|
90 | struct UBO_VP_mats {
|
---|
91 | alignas(16) mat4 view;
|
---|
92 | alignas(16) mat4 proj;
|
---|
93 | };
|
---|
94 |
|
---|
95 | struct UBO_Explosion {
|
---|
96 | alignas(16) mat4 view;
|
---|
97 | alignas(16) mat4 proj;
|
---|
98 | alignas(4) float cur_time;
|
---|
99 | };
|
---|
100 |
|
---|
101 | // TODO: Use this struct for uniform buffers as well and probably combine it with the VulkanBuffer class
|
---|
102 | // Also, probably better to make this a vector of structs where each struct
|
---|
103 | // has a VkBuffer, VkDeviceMemory, and VkDescriptorBufferInfo
|
---|
104 | // TODO: Maybe change the structure here since VkDescriptorBufferInfo already stores a reference to the VkBuffer
|
---|
105 | struct BufferSet {
|
---|
106 | vector<VkBuffer> buffers;
|
---|
107 | vector<VkDeviceMemory> memory;
|
---|
108 | vector<VkDescriptorBufferInfo> infoSet;
|
---|
109 | };
|
---|
110 |
|
---|
111 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
112 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
113 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
114 | template<class VertexType, class SSBOType>
|
---|
115 | struct SceneObject {
|
---|
116 | vector<VertexType> vertices;
|
---|
117 | vector<uint16_t> indices;
|
---|
118 | SSBOType ssbo;
|
---|
119 |
|
---|
120 | mat4 model_base;
|
---|
121 | mat4 model_transform;
|
---|
122 |
|
---|
123 | bool modified;
|
---|
124 |
|
---|
125 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
126 | // parent class
|
---|
127 | vec3 center; // currently only matters for asteroids
|
---|
128 | float radius; // currently only matters for asteroids
|
---|
129 | SceneObject<ModelVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
---|
130 | };
|
---|
131 |
|
---|
132 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
133 | // Could probably use the same approach to make indices optional
|
---|
134 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
135 | // them mamdatory
|
---|
136 |
|
---|
137 |
|
---|
138 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
---|
139 |
|
---|
140 | struct BaseEffectOverTime {
|
---|
141 | bool deleted;
|
---|
142 |
|
---|
143 | virtual void applyEffect(float curTime) = 0;
|
---|
144 |
|
---|
145 | BaseEffectOverTime() :
|
---|
146 | deleted(false) {
|
---|
147 | }
|
---|
148 |
|
---|
149 | virtual ~BaseEffectOverTime() {
|
---|
150 | }
|
---|
151 | };
|
---|
152 |
|
---|
153 | template<class VertexType, class SSBOType>
|
---|
154 | struct EffectOverTime : public BaseEffectOverTime {
|
---|
155 | GraphicsPipeline_Vulkan<VertexType>& pipeline;
|
---|
156 | vector<SceneObject<VertexType, SSBOType>>& objects;
|
---|
157 | unsigned int objectIndex;
|
---|
158 | size_t effectedFieldOffset;
|
---|
159 | float startValue;
|
---|
160 | float startTime;
|
---|
161 | float changePerSecond;
|
---|
162 |
|
---|
163 | EffectOverTime(GraphicsPipeline_Vulkan<VertexType>& pipeline, vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
164 | unsigned int objectIndex, size_t effectedFieldOffset, float startTime, float changePerSecond)
|
---|
165 | : pipeline(pipeline)
|
---|
166 | , objects(objects)
|
---|
167 | , objectIndex(objectIndex)
|
---|
168 | , effectedFieldOffset(effectedFieldOffset)
|
---|
169 | , startTime(startTime)
|
---|
170 | , changePerSecond(changePerSecond) {
|
---|
171 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
172 |
|
---|
173 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
174 | ssboOffset + effectedFieldOffset;
|
---|
175 |
|
---|
176 | startValue = *reinterpret_cast<float*>(effectedFieldPtr);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void applyEffect(float curTime) {
|
---|
180 | if (objects[objectIndex].ssbo.deleted) {
|
---|
181 | this->deleted = true;
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
186 |
|
---|
187 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
188 | ssboOffset + effectedFieldOffset;
|
---|
189 |
|
---|
190 | *reinterpret_cast<float*>(effectedFieldPtr) = startValue + (curTime - startTime) * changePerSecond;
|
---|
191 |
|
---|
192 | objects[objectIndex].modified = true;
|
---|
193 | }
|
---|
194 | };
|
---|
195 |
|
---|
196 | // TODO: Maybe move this to a different header
|
---|
197 |
|
---|
198 | enum UIValueType {
|
---|
199 | UIVALUE_INT,
|
---|
200 | UIVALUE_DOUBLE,
|
---|
201 | };
|
---|
202 |
|
---|
203 | struct UIValue {
|
---|
204 | UIValueType type;
|
---|
205 | string label;
|
---|
206 | void* value;
|
---|
207 |
|
---|
208 | UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
|
---|
209 | };
|
---|
210 |
|
---|
211 | /* TODO: The following syntax (note the const keyword) means the function will not modify
|
---|
212 | * its params. I should use this where appropriate
|
---|
213 | *
|
---|
214 | * [return-type] [func-name](params...) const { ... }
|
---|
215 | */
|
---|
216 |
|
---|
217 | class VulkanGame {
|
---|
218 |
|
---|
219 | public:
|
---|
220 |
|
---|
221 | VulkanGame();
|
---|
222 | ~VulkanGame();
|
---|
223 |
|
---|
224 | void run(int width, int height, unsigned char guiFlags);
|
---|
225 |
|
---|
226 | private:
|
---|
227 |
|
---|
228 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
229 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
230 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
231 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
232 | void* pUserData);
|
---|
233 |
|
---|
234 | // TODO: Maybe pass these in as parameters to some Camera class
|
---|
235 | const float NEAR_CLIP = 0.1f;
|
---|
236 | const float FAR_CLIP = 100.0f;
|
---|
237 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 deg
|
---|
238 |
|
---|
239 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
---|
240 | const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
|
---|
241 |
|
---|
242 | bool done;
|
---|
243 |
|
---|
244 | vec3 cam_pos;
|
---|
245 |
|
---|
246 | // TODO: Good place to start using smart pointers
|
---|
247 | GameGui* gui;
|
---|
248 |
|
---|
249 | SDL_version sdlVersion;
|
---|
250 | SDL_Window* window = nullptr;
|
---|
251 |
|
---|
252 | int drawableWidth, drawableHeight;
|
---|
253 |
|
---|
254 | VkInstance instance;
|
---|
255 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
256 | VkSurfaceKHR vulkanSurface;
|
---|
257 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
258 | VkDevice device;
|
---|
259 |
|
---|
260 | VkQueue graphicsQueue;
|
---|
261 | VkQueue presentQueue;
|
---|
262 |
|
---|
263 | // TODO: Maybe make a swapchain struct for convenience
|
---|
264 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
---|
265 | VkPresentModeKHR swapChainPresentMode;
|
---|
266 | VkExtent2D swapChainExtent;
|
---|
267 | uint32_t swapChainMinImageCount;
|
---|
268 | uint32_t swapChainImageCount;
|
---|
269 | VkSwapchainKHR swapChain;
|
---|
270 | vector<VkImage> swapChainImages;
|
---|
271 | vector<VkImageView> swapChainImageViews;
|
---|
272 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
273 |
|
---|
274 | VkRenderPass renderPass;
|
---|
275 |
|
---|
276 | VkCommandPool resourceCommandPool;
|
---|
277 |
|
---|
278 | vector<VkCommandPool> commandPools;
|
---|
279 | vector<VkCommandBuffer> commandBuffers;
|
---|
280 |
|
---|
281 | VulkanImage depthImage;
|
---|
282 |
|
---|
283 | // These are per frame
|
---|
284 | vector<VkSemaphore> imageAcquiredSemaphores;
|
---|
285 | vector<VkSemaphore> renderCompleteSemaphores;
|
---|
286 |
|
---|
287 | // These are per swap chain image
|
---|
288 | vector<VkFence> inFlightFences;
|
---|
289 |
|
---|
290 | uint32_t imageIndex;
|
---|
291 | uint32_t currentFrame;
|
---|
292 |
|
---|
293 | bool shouldRecreateSwapChain;
|
---|
294 |
|
---|
295 | VkSampler textureSampler;
|
---|
296 |
|
---|
297 | VulkanImage floorTextureImage;
|
---|
298 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
299 |
|
---|
300 | VulkanImage laserTextureImage;
|
---|
301 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
---|
302 |
|
---|
303 | mat4 viewMat, projMat;
|
---|
304 |
|
---|
305 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
306 | VkDescriptorPool imguiDescriptorPool;
|
---|
307 |
|
---|
308 | // TODO: Probably restructure the GraphicsPipeline_Vulkan class based on what I learned about descriptors and textures
|
---|
309 | // while working on graphics-library. Double-check exactly what this was and note it down here.
|
---|
310 | // Basically, I think the point was that if I have several modesl that all use the same shaders and, therefore,
|
---|
311 | // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
|
---|
312 | // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
|
---|
313 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
314 | GraphicsPipeline_Vulkan<ModelVertex> shipPipeline;
|
---|
315 | GraphicsPipeline_Vulkan<ModelVertex> asteroidPipeline;
|
---|
316 | GraphicsPipeline_Vulkan<LaserVertex> laserPipeline;
|
---|
317 | GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline;
|
---|
318 |
|
---|
319 | BufferSet storageBuffers_modelPipeline;
|
---|
320 | VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
|
---|
321 | BufferSet uniformBuffers_modelPipeline;
|
---|
322 |
|
---|
323 | BufferSet storageBuffers_shipPipeline;
|
---|
324 | VulkanBuffer<SSBO_ModelObject> objects_shipPipeline;
|
---|
325 | BufferSet uniformBuffers_shipPipeline;
|
---|
326 |
|
---|
327 | BufferSet storageBuffers_asteroidPipeline;
|
---|
328 | VulkanBuffer<SSBO_Asteroid> objects_asteroidPipeline;
|
---|
329 | BufferSet uniformBuffers_asteroidPipeline;
|
---|
330 |
|
---|
331 | BufferSet storageBuffers_laserPipeline;
|
---|
332 | VulkanBuffer<SSBO_Laser> objects_laserPipeline;
|
---|
333 | BufferSet uniformBuffers_laserPipeline;
|
---|
334 |
|
---|
335 | BufferSet storageBuffers_explosionPipeline;
|
---|
336 | VulkanBuffer<SSBO_Explosion> objects_explosionPipeline;
|
---|
337 | BufferSet uniformBuffers_explosionPipeline;
|
---|
338 |
|
---|
339 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
340 | // per pipeline.
|
---|
341 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
342 | // the objects vector, the ubo, and the ssbo
|
---|
343 |
|
---|
344 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
345 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
346 |
|
---|
347 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
348 |
|
---|
349 | UBO_VP_mats object_VP_mats;
|
---|
350 |
|
---|
351 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> shipObjects;
|
---|
352 |
|
---|
353 | UBO_VP_mats ship_VP_mats;
|
---|
354 |
|
---|
355 | vector<SceneObject<ModelVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
356 |
|
---|
357 | UBO_VP_mats asteroid_VP_mats;
|
---|
358 |
|
---|
359 | vector<SceneObject<LaserVertex, SSBO_Laser>> laserObjects;
|
---|
360 |
|
---|
361 | UBO_VP_mats laser_VP_mats;
|
---|
362 |
|
---|
363 | vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
|
---|
364 |
|
---|
365 | UBO_Explosion explosion_UBO;
|
---|
366 |
|
---|
367 | vector<BaseEffectOverTime*> effects;
|
---|
368 |
|
---|
369 | float shipSpeed = 0.5f;
|
---|
370 | float asteroidSpeed = 2.0f;
|
---|
371 |
|
---|
372 | float spawnRate_asteroid = 0.5;
|
---|
373 | float lastSpawn_asteroid;
|
---|
374 |
|
---|
375 | unsigned int leftLaserIdx = -1;
|
---|
376 | EffectOverTime<ModelVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
---|
377 |
|
---|
378 | unsigned int rightLaserIdx = -1;
|
---|
379 | EffectOverTime<ModelVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
---|
380 |
|
---|
381 | /*** High-level vars ***/
|
---|
382 |
|
---|
383 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
384 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
385 |
|
---|
386 | map<string, vector<UIValue>> valueLists;
|
---|
387 |
|
---|
388 | int score;
|
---|
389 | float fps;
|
---|
390 |
|
---|
391 | // TODO: Make a separate TImer class
|
---|
392 | time_point<steady_clock> startTime;
|
---|
393 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
394 |
|
---|
395 | int frameCount;
|
---|
396 |
|
---|
397 | /*** Functions ***/
|
---|
398 |
|
---|
399 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
400 | void initVulkan();
|
---|
401 | void initGraphicsPipelines();
|
---|
402 | void initMatrices();
|
---|
403 | void renderLoop();
|
---|
404 | void updateScene();
|
---|
405 | void cleanup();
|
---|
406 |
|
---|
407 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
408 | void setupDebugMessenger();
|
---|
409 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
410 | void createVulkanSurface();
|
---|
411 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
412 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
413 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
414 | const vector<const char*>& deviceExtensions);
|
---|
415 | void chooseSwapChainProperties();
|
---|
416 | void createSwapChain();
|
---|
417 | void createImageViews();
|
---|
418 | void createResourceCommandPool();
|
---|
419 | void createImageResources();
|
---|
420 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
421 | void createRenderPass();
|
---|
422 | void createCommandPools();
|
---|
423 | void createFramebuffers();
|
---|
424 | void createCommandBuffers();
|
---|
425 | void createSyncObjects();
|
---|
426 |
|
---|
427 | void createTextureSampler();
|
---|
428 |
|
---|
429 | void initImGuiOverlay();
|
---|
430 | void cleanupImGuiOverlay();
|
---|
431 |
|
---|
432 | // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
|
---|
433 |
|
---|
434 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties,
|
---|
435 | BufferSet& set);
|
---|
436 |
|
---|
437 | template<class VertexType, class SSBOType>
|
---|
438 | void resizeBufferSet(BufferSet& set, VulkanBuffer<SSBOType>& buffer,
|
---|
439 | GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool,
|
---|
440 | VkQueue graphicsQueue);
|
---|
441 |
|
---|
442 | template<class SSBOType>
|
---|
443 | void updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo);
|
---|
444 |
|
---|
445 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
446 | // stop using objects.back() to access the object that was just created
|
---|
447 | template<class VertexType, class SSBOType>
|
---|
448 | SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
449 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
450 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
451 | SSBOType ssbo);
|
---|
452 |
|
---|
453 | template<class VertexType>
|
---|
454 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
455 |
|
---|
456 | template<class VertexType>
|
---|
457 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
458 |
|
---|
459 | template<class VertexType, class SSBOType>
|
---|
460 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
461 |
|
---|
462 | template<class VertexType, class SSBOType>
|
---|
463 | void updateObject(SceneObject<VertexType, SSBOType>& obj);
|
---|
464 |
|
---|
465 | template<class VertexType, class SSBOType>
|
---|
466 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
467 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
---|
468 |
|
---|
469 | void addLaser(vec3 start, vec3 end, vec3 color, float width);
|
---|
470 | void translateLaser(size_t index, const vec3& translation);
|
---|
471 | void updateLaserTarget(size_t index);
|
---|
472 | bool getLaserAndAsteroidIntersection(SceneObject<ModelVertex, SSBO_Asteroid>& asteroid,
|
---|
473 | vec3& start, vec3& end, vec3& intersection);
|
---|
474 |
|
---|
475 | void addExplosion(mat4 model_mat, float duration, float cur_time);
|
---|
476 |
|
---|
477 | void renderFrame(ImDrawData* draw_data);
|
---|
478 | void presentFrame();
|
---|
479 |
|
---|
480 | void recreateSwapChain();
|
---|
481 |
|
---|
482 | void cleanupSwapChain();
|
---|
483 |
|
---|
484 | /*** High-level functions ***/
|
---|
485 |
|
---|
486 | void renderMainScreen(int width, int height);
|
---|
487 | void renderGameScreen(int width, int height);
|
---|
488 |
|
---|
489 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
490 | void renderGuiValueList(vector<UIValue>& values);
|
---|
491 |
|
---|
492 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
493 | void quitGame();
|
---|
494 | };
|
---|
495 |
|
---|
496 | // Start of specialized no-op functions
|
---|
497 |
|
---|
498 | template<>
|
---|
499 | inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
|
---|
500 | }
|
---|
501 |
|
---|
502 | // End of specialized no-op functions
|
---|
503 |
|
---|
504 | template<class VertexType, class SSBOType>
|
---|
505 | void VulkanGame::resizeBufferSet(BufferSet& set, VulkanBuffer<SSBOType>& buffer,
|
---|
506 | GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool,
|
---|
507 | VkQueue graphicsQueue) {
|
---|
508 | size_t numObjects = buffer.numObjects < buffer.capacity ? buffer.numObjects : buffer.capacity;
|
---|
509 |
|
---|
510 | do {
|
---|
511 | buffer.capacity *= 2;
|
---|
512 | } while (buffer.capacity < buffer.numObjects);
|
---|
513 |
|
---|
514 | VkDeviceSize bufferSize = buffer.capacity * sizeof(SSBOType);
|
---|
515 |
|
---|
516 | for (size_t i = 0; i < set.buffers.size(); i++) {
|
---|
517 | VkBuffer newStorageBuffer;
|
---|
518 | VkDeviceMemory newStorageBufferMemory;
|
---|
519 |
|
---|
520 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize,
|
---|
521 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
---|
522 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
523 | newStorageBuffer, newStorageBufferMemory);
|
---|
524 |
|
---|
525 | VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer,
|
---|
526 | 0, 0, numObjects * sizeof(SSBOType), graphicsQueue);
|
---|
527 |
|
---|
528 | vkDestroyBuffer(device, set.buffers[i], nullptr);
|
---|
529 | vkFreeMemory(device, set.memory[i], nullptr);
|
---|
530 |
|
---|
531 | set.buffers[i] = newStorageBuffer;
|
---|
532 | set.memory[i] = newStorageBufferMemory;
|
---|
533 |
|
---|
534 | set.infoSet[i].buffer = set.buffers[i];
|
---|
535 | set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
|
---|
536 | set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
|
---|
537 | }
|
---|
538 |
|
---|
539 | // Assume the SSBO is always the 2nd binding
|
---|
540 | // TODO: Figure out a way to make this more flexible
|
---|
541 | pipeline.updateDescriptorInfo(1, &set.infoSet, swapChainImages.size());
|
---|
542 | }
|
---|
543 |
|
---|
544 | // TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
|
---|
545 | template<class SSBOType>
|
---|
546 | void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
|
---|
547 | for (size_t i = 0; i < set.memory.size(); i++) {
|
---|
548 | VulkanUtils::copyDataToMemory(device, ssbo, set.memory[i], objIndex * sizeof(SSBOType));
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
553 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
554 | // Figure out a better way to allow the model matrix to be set during object creation
|
---|
555 | template<class VertexType, class SSBOType>
|
---|
556 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
557 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
558 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
559 | SSBOType ssbo) {
|
---|
560 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
561 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
562 | size_t numVertices = pipeline.getNumVertices();
|
---|
563 |
|
---|
564 | for (uint16_t& idx : indices) {
|
---|
565 | idx += numVertices;
|
---|
566 | }
|
---|
567 |
|
---|
568 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
---|
569 |
|
---|
570 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
571 |
|
---|
572 | // TODO: Specify whether to center the object outside of this function or, worst case, maybe
|
---|
573 | // with a boolean being passed in here, so that I don't have to rely on checking the specific object
|
---|
574 | // type
|
---|
575 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
576 | centerObject(obj);
|
---|
577 | }
|
---|
578 |
|
---|
579 | pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
|
---|
580 |
|
---|
581 | return obj;
|
---|
582 | }
|
---|
583 |
|
---|
584 | template<class VertexType>
|
---|
585 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
586 | for (VertexType& vertex : vertices) {
|
---|
587 | vertex.objIndex = objIndex;
|
---|
588 | }
|
---|
589 |
|
---|
590 | return vertices;
|
---|
591 | }
|
---|
592 |
|
---|
593 | // This function sets all the normals for a face to be parallel
|
---|
594 | // This is good for models that should have distinct faces, but bad for models that should appear smooth
|
---|
595 | // Maybe add an option to set all copies of a point to have the same normal and have the direction of
|
---|
596 | // that normal be the weighted average of all the faces it is a part of, where the weight from each face
|
---|
597 | // is its surface area.
|
---|
598 |
|
---|
599 | // TODO: Since the current approach to normal calculation basicaly makes indexed drawing useless, see if it's
|
---|
600 | // feasible to automatically enable/disable indexed drawing based on which approach is used
|
---|
601 | template<class VertexType>
|
---|
602 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
603 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
604 | vec3 p1 = vertices[i].pos;
|
---|
605 | vec3 p2 = vertices[i + 1].pos;
|
---|
606 | vec3 p3 = vertices[i + 2].pos;
|
---|
607 |
|
---|
608 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
609 |
|
---|
610 | // Add the same normal for all 3 vertices
|
---|
611 | vertices[i].normal = normal;
|
---|
612 | vertices[i + 1].normal = normal;
|
---|
613 | vertices[i + 2].normal = normal;
|
---|
614 | }
|
---|
615 |
|
---|
616 | return vertices;
|
---|
617 | }
|
---|
618 |
|
---|
619 | template<class VertexType, class SSBOType>
|
---|
620 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
621 | vector<VertexType>& vertices = object.vertices;
|
---|
622 |
|
---|
623 | float min_x = vertices[0].pos.x;
|
---|
624 | float max_x = vertices[0].pos.x;
|
---|
625 | float min_y = vertices[0].pos.y;
|
---|
626 | float max_y = vertices[0].pos.y;
|
---|
627 | float min_z = vertices[0].pos.z;
|
---|
628 | float max_z = vertices[0].pos.z;
|
---|
629 |
|
---|
630 | // start from the second point
|
---|
631 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
632 | vec3& pos = vertices[i].pos;
|
---|
633 |
|
---|
634 | if (min_x > pos.x) {
|
---|
635 | min_x = pos.x;
|
---|
636 | } else if (max_x < pos.x) {
|
---|
637 | max_x = pos.x;
|
---|
638 | }
|
---|
639 |
|
---|
640 | if (min_y > pos.y) {
|
---|
641 | min_y = pos.y;
|
---|
642 | } else if (max_y < pos.y) {
|
---|
643 | max_y = pos.y;
|
---|
644 | }
|
---|
645 |
|
---|
646 | if (min_z > pos.z) {
|
---|
647 | min_z = pos.z;
|
---|
648 | } else if (max_z < pos.z) {
|
---|
649 | max_z = pos.z;
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
654 |
|
---|
655 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
656 | vertices[i].pos -= center;
|
---|
657 | }
|
---|
658 |
|
---|
659 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
660 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
661 |
|
---|
662 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
663 | }
|
---|
664 |
|
---|
665 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
666 | template<class VertexType, class SSBOType>
|
---|
667 | void VulkanGame::updateObject(SceneObject<VertexType, SSBOType>& obj) {
|
---|
668 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
669 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
670 |
|
---|
671 | obj.modified = false;
|
---|
672 | }
|
---|
673 |
|
---|
674 | template<class VertexType, class SSBOType>
|
---|
675 | void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
676 | SceneObject<VertexType, SSBOType>& obj, size_t index) {
|
---|
677 | pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
|
---|
678 | }
|
---|
679 |
|
---|
680 | #endif // _VULKAN_GAME_H
|
---|