Changeset 2d87297 in opengl-game
- Timestamp:
- Feb 16, 2020, 8:22:40 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- d25381b
- Parents:
- 5a1ace0
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.hpp
r5a1ace0 r2d87297 31 31 }; 32 32 33 template<class VertexType >33 template<class VertexType, class SSBOType> 34 34 class GraphicsPipeline_Vulkan : public GraphicsPipeline { 35 35 public: … … 99 99 /*** PUBLIC METHODS ***/ 100 100 101 template<class VertexType >102 GraphicsPipeline_Vulkan<VertexType >::GraphicsPipeline_Vulkan() {101 template<class VertexType, class SSBOType> 102 GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan() { 103 103 } 104 104 105 105 // TODO: Verify that vertex capacity and index capacity are both > 0 106 template<class VertexType> 107 GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, 106 template<class VertexType, class SSBOType> 107 GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan( 108 VkPhysicalDevice physicalDevice, VkDevice device, 108 109 VkRenderPass renderPass, Viewport viewport, size_t vertexCapacity, size_t indexCapacity) { 109 110 this->physicalDevice = physicalDevice; … … 134 135 } 135 136 136 template<class VertexType >137 GraphicsPipeline_Vulkan<VertexType >::~GraphicsPipeline_Vulkan() {138 } 139 140 template<class VertexType >141 size_t GraphicsPipeline_Vulkan<VertexType >::getNumVertices() {137 template<class VertexType, class SSBOType> 138 GraphicsPipeline_Vulkan<VertexType, SSBOType>::~GraphicsPipeline_Vulkan() { 139 } 140 141 template<class VertexType, class SSBOType> 142 size_t GraphicsPipeline_Vulkan<VertexType, SSBOType>::getNumVertices() { 142 143 return numVertices; 143 144 } 144 145 145 template<class VertexType >146 void GraphicsPipeline_Vulkan<VertexType >::updateRenderPass(VkRenderPass renderPass) {146 template<class VertexType, class SSBOType> 147 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::updateRenderPass(VkRenderPass renderPass) { 147 148 this->renderPass = renderPass; 148 149 } 149 150 150 template<class VertexType >151 void GraphicsPipeline_Vulkan<VertexType >::addAttribute(VkFormat format, size_t offset) {151 template<class VertexType, class SSBOType> 152 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addAttribute(VkFormat format, size_t offset) { 152 153 VkVertexInputAttributeDescription attributeDesc = {}; 153 154 … … 160 161 } 161 162 162 template<class VertexType >163 void GraphicsPipeline_Vulkan<VertexType >::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {163 template<class VertexType, class SSBOType> 164 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) { 164 165 this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr }); 165 166 } 166 167 167 template<class VertexType >168 void GraphicsPipeline_Vulkan<VertexType >::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {168 template<class VertexType, class SSBOType> 169 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) { 169 170 this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData }); 170 171 } 171 172 172 template<class VertexType >173 void GraphicsPipeline_Vulkan<VertexType >::createPipeline(string vertShaderFile, string fragShaderFile) {173 template<class VertexType, class SSBOType> 174 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createPipeline(string vertShaderFile, string fragShaderFile) { 174 175 vector<char> vertShaderCode = readFile(vertShaderFile); 175 176 vector<char> fragShaderCode = readFile(fragShaderFile); … … 308 309 } 309 310 310 template<class VertexType >311 void GraphicsPipeline_Vulkan<VertexType >::createDescriptorSetLayout() {311 template<class VertexType, class SSBOType> 312 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSetLayout() { 312 313 vector<VkDescriptorSetLayoutBinding> bindings(this->descriptorInfoList.size()); 313 314 … … 330 331 } 331 332 332 template<class VertexType >333 void GraphicsPipeline_Vulkan<VertexType >::createDescriptorPool(vector<VkImage>& swapChainImages) {333 template<class VertexType, class SSBOType> 334 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorPool(vector<VkImage>& swapChainImages) { 334 335 vector<VkDescriptorPoolSize> poolSizes(this->descriptorInfoList.size()); 335 336 … … 350 351 } 351 352 352 template<class VertexType >353 void GraphicsPipeline_Vulkan<VertexType >::createDescriptorSets(vector<VkImage>& swapChainImages) {353 template<class VertexType, class SSBOType> 354 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSets(vector<VkImage>& swapChainImages) { 354 355 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), this->descriptorSetLayout); 355 356 … … 396 397 } 397 398 398 template<class VertexType >399 void GraphicsPipeline_Vulkan<VertexType >::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {399 template<class VertexType, class SSBOType> 400 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) { 400 401 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); 401 402 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, … … 411 412 } 412 413 413 template<class VertexType >414 void GraphicsPipeline_Vulkan<VertexType >::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices,414 template<class VertexType, class SSBOType> 415 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices, 415 416 VkCommandPool commandPool, VkQueue graphicsQueue) { 416 417 … … 431 432 } 432 433 433 template<class VertexType >434 void GraphicsPipeline_Vulkan<VertexType >::cleanup() {434 template<class VertexType, class SSBOType> 435 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanup() { 435 436 vkDestroyPipeline(device, pipeline, nullptr); 436 437 vkDestroyDescriptorPool(device, descriptorPool, nullptr); … … 438 439 } 439 440 440 template<class VertexType >441 void GraphicsPipeline_Vulkan<VertexType >::cleanupBuffers() {441 template<class VertexType, class SSBOType> 442 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanupBuffers() { 442 443 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); 443 444 … … 450 451 /*** PRIVATE METHODS ***/ 451 452 452 template<class VertexType >453 VkShaderModule GraphicsPipeline_Vulkan<VertexType >::createShaderModule(const vector<char>& code) {453 template<class VertexType, class SSBOType> 454 VkShaderModule GraphicsPipeline_Vulkan<VertexType, SSBOType>::createShaderModule(const vector<char>& code) { 454 455 VkShaderModuleCreateInfo createInfo = {}; 455 456 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; … … 465 466 } 466 467 467 template<class VertexType >468 vector<char> GraphicsPipeline_Vulkan<VertexType >::readFile(const string& filename) {468 template<class VertexType, class SSBOType> 469 vector<char> GraphicsPipeline_Vulkan<VertexType, SSBOType>::readFile(const string& filename) { 469 470 ifstream file(filename, ios::ate | ios::binary); 470 471 … … 484 485 } 485 486 486 template<class VertexType >487 void GraphicsPipeline_Vulkan<VertexType >::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {487 template<class VertexType, class SSBOType> 488 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { 488 489 VkBuffer newVertexBuffer; 489 490 VkDeviceMemory newVertexBufferMemory; … … 503 504 } 504 505 505 template<class VertexType >506 void GraphicsPipeline_Vulkan<VertexType >::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {506 template<class VertexType, class SSBOType> 507 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { 507 508 VkBuffer newIndexBuffer; 508 509 VkDeviceMemory newIndexBufferMemory; -
vulkan-game.cpp
r5a1ace0 r2d87297 202 202 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 203 203 // TODO: Calculate the size of this buffer (and all the other SSBOs) based on the number of objects 204 createBufferSet(10 * sizeof(S BO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,204 createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 205 205 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 206 206 … … 221 221 }), { 222 222 0, 1, 2, 2, 3, 0 223 }, { 224 mat4(1.0f) 223 225 }); 224 226 … … 231 233 }), { 232 234 0, 1, 2, 2, 3, 0 235 }, { 236 mat4(1.0f) 233 237 }); 234 238 … … 252 256 }, { 253 257 0, 1, 2, 2, 3, 0 254 } );258 }, {}); 255 259 256 260 overlayPipeline.createDescriptorSetLayout(); … … 266 270 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 267 271 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 268 createBufferSet(10 * sizeof(S BO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,272 createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 269 273 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 270 274 … … 506 510 132, 133, 134, 507 511 135, 136, 137, 512 }, { 513 mat4(1.0f) 508 514 }); 509 515 … … 520 526 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 521 527 uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline); 522 createBufferSet(10 * sizeof(S BO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,528 createBufferSet(10 * sizeof(SSBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 523 529 storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline); 524 530 … … 587 593 24, 25, 26, 27, 28, 29, 588 594 30, 31, 32, 33, 34, 35, 595 }, { 596 mat4(1.0f), 597 10.0f, 598 0 589 599 }); 590 600 … … 620 630 621 631 void VulkanGame::initGraphicsPipelines() { 622 modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass, 632 overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>(physicalDevice, device, renderPass, 633 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6); 634 635 modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>(physicalDevice, device, renderPass, 623 636 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24); 624 637 625 overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass, 626 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6); 627 628 shipPipeline = GraphicsPipeline_Vulkan<ShipVertex>(physicalDevice, device, renderPass, 638 shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>(physicalDevice, device, renderPass, 629 639 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 138, 138); 630 640 631 asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex >(physicalDevice, device, renderPass,641 asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>(physicalDevice, device, renderPass, 632 642 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 24, 36); 633 643 } … … 707 717 }), { 708 718 0, 1, 2, 2, 3, 0 719 }, { 720 mat4(1.0f) 709 721 }); 710 722 … … 1449 1461 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1450 1462 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 1451 createBufferSet(10 * sizeof(S BO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,1463 createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1452 1464 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 1453 1465 … … 1464 1476 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1465 1477 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 1466 createBufferSet(10 * sizeof(S BO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,1478 createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1467 1479 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 1468 1480 … … 1474 1486 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1475 1487 uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline); 1476 createBufferSet(10 * sizeof(S BO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,1488 createBufferSet(10 * sizeof(SSBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1477 1489 storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline); 1478 1490 -
vulkan-game.hpp
r5a1ace0 r2d87297 50 50 // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference 51 51 // TODO: Create a typedef for index type so I can easily change uin16_t to something else later 52 template<class VertexType >52 template<class VertexType, class SSBOType> 53 53 struct SceneObject { 54 54 vector<VertexType> vertices; 55 55 vector<uint16_t> indices; 56 SSBOType ssbo; 56 57 57 58 mat4 model_base; … … 64 65 }; 65 66 66 struct S BO_SceneObject {67 struct SSBO_ModelObject { 67 68 alignas(16) mat4 model; 68 69 }; 69 70 70 struct S BO_Asteroid {71 struct SSBO_Asteroid { 71 72 alignas(16) mat4 model; 72 73 alignas(4) float hp; … … 146 147 // TODO: Create a struct that holds the buffers, memory, and info objects (Probably in VulkanUtils) 147 148 148 GraphicsPipeline_Vulkan<OverlayVertex > overlayPipeline;149 150 vector<SceneObject<OverlayVertex >> overlayObjects;149 GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline; 150 151 vector<SceneObject<OverlayVertex, void*>> overlayObjects; 151 152 152 153 // TODO: Rename all the variables related to modelPipeline to use the same pipelie name 153 154 154 GraphicsPipeline_Vulkan<ModelVertex > modelPipeline;155 156 vector<SceneObject<ModelVertex >> modelObjects;155 GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline; 156 157 vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects; 157 158 158 159 vector<VkBuffer> uniformBuffers_scenePipeline; … … 165 166 166 167 UBO_VP_mats object_VP_mats; 167 S BO_SceneObject so_Object;168 169 GraphicsPipeline_Vulkan<ShipVertex > shipPipeline;170 171 vector<SceneObject<ShipVertex >> shipObjects;168 SSBO_ModelObject so_Object; 169 170 GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline; 171 172 vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects; 172 173 173 174 vector<VkBuffer> uniformBuffers_shipPipeline; … … 180 181 181 182 UBO_VP_mats ship_VP_mats; 182 S BO_SceneObject so_Ship;183 184 GraphicsPipeline_Vulkan<AsteroidVertex > asteroidPipeline;185 186 vector<SceneObject<AsteroidVertex >> asteroidObjects;183 SSBO_ModelObject so_Ship; 184 185 GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline; 186 187 vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects; 187 188 188 189 vector<VkBuffer> uniformBuffers_asteroidPipeline; … … 195 196 196 197 UBO_VP_mats asteroid_VP_mats; 197 S BO_Asteroid so_Asteroid;198 SSBO_Asteroid so_Asteroid; 198 199 199 200 Uint64 curTime, prevTime; … … 231 232 void createSyncObjects(); 232 233 233 template<class VertexType> 234 void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline, 235 const vector<VertexType>& vertices, vector<uint16_t> indices); 234 template<class VertexType, class SSBOType> 235 void addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 236 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 237 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo); 236 238 237 239 template<class VertexType> … … 244 246 vector<VertexType> centerObject(vector<VertexType> vertices); 245 247 246 template<class VertexType >247 void transformObject(SceneObject<VertexType >& obj, mat4 mat);248 template<class VertexType, class SSBOType> 249 void transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat); 248 250 249 251 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, … … 261 263 }; 262 264 263 template<class VertexType> 264 void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline, 265 const vector<VertexType>& vertices, vector<uint16_t> indices) { 265 template<class VertexType, class SSBOType> 266 void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 267 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 268 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo) { 266 269 size_t numVertices = pipeline.getNumVertices(); 267 270 … … 270 273 } 271 274 272 objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });275 objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) }); 273 276 274 277 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue); … … 341 344 } 342 345 343 template<class VertexType >344 void VulkanGame::transformObject(SceneObject<VertexType >& obj, mat4 mat) {346 template<class VertexType, class SSBOType> 347 void VulkanGame::transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat) { 345 348 obj.model_transform = mat * obj.model_transform; 346 349 }
Note:
See TracChangeset
for help on using the changeset viewer.