source: opengl-game/vulkan-game.hpp@ 8dcbf62

feature/imgui-sdl
Last change on this file since 8dcbf62 was 8dcbf62, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Add some functionality to VulkanBuffer, and modify VulkanGame::addObject() to call VulkanBuffer::add()

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