Changeset 860a0da in opengl-game for graphics-pipeline_vulkan.hpp
- Timestamp:
- Feb 18, 2020, 9:36:51 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 2da64ef
- Parents:
- d25381b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.hpp
rd25381b r860a0da 5 5 6 6 #include <fstream> 7 #include <iostream> 7 8 #include <stdexcept> 8 9 #include <vector> … … 31 32 }; 32 33 34 struct StorageBufferSet { 35 vector<VkBuffer> buffers; 36 vector<VkDeviceMemory> memory; 37 vector<VkDescriptorBufferInfo> infoSet; 38 }; 39 33 40 template<class VertexType, class SSBOType> 34 41 class GraphicsPipeline_Vulkan : public GraphicsPipeline { 35 42 public: 36 43 GraphicsPipeline_Vulkan(); 44 45 // TODO: swapChainImages is only ever used to get its size. Check how that is determined and, 46 // if it will never change, just pass it in the constructor and save it 47 // If it does change, I could add an updateSwapchainImageCount() function 37 48 GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass, 38 Viewport viewport, size_t vertexCapacity, size_t indexCapacity); 49 Viewport viewport, vector<VkImage>& swapChainImages, 50 size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity); 39 51 ~GraphicsPipeline_Vulkan(); 40 52 … … 46 58 47 59 void addAttribute(VkFormat format, size_t offset); 60 61 void addStorageDescriptor(); 48 62 49 63 void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData); … … 62 76 void cleanup(); 63 77 void cleanupBuffers(); 78 79 StorageBufferSet storageBufferSet; 64 80 65 81 private: … … 90 106 VkDeviceMemory indexBufferMemory; 91 107 108 size_t numObjects; 109 size_t objectCapacity; 110 92 111 VkShaderModule createShaderModule(const vector<char>& code); 93 112 vector<char> readFile(const string& filename); … … 107 126 GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan( 108 127 VkPhysicalDevice physicalDevice, VkDevice device, 109 VkRenderPass renderPass, Viewport viewport, size_t vertexCapacity, size_t indexCapacity) { 128 VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages, 129 size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity) { 110 130 this->physicalDevice = physicalDevice; 111 131 this->device = device; … … 133 153 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 134 154 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory); 135 } 136 155 156 this->numObjects = 0; 157 this->objectCapacity = objectCapacity; 158 159 // Hacky way to allow an SSBO to be optional 160 // Specifying void* as the SSBOType will skip allocating the related buffers 161 if (!is_same_v<SSBOType, void*>) { 162 VkDeviceSize bufferSize = objectCapacity * sizeof(SSBOType); 163 cout << "NUM SWAP CHAIN IMAGES: " << swapChainImages.size() << endl; 164 165 storageBufferSet.buffers.resize(swapChainImages.size()); 166 storageBufferSet.memory.resize(swapChainImages.size()); 167 storageBufferSet.infoSet.resize(swapChainImages.size()); 168 169 for (size_t i = 0; i < swapChainImages.size(); i++) { 170 VulkanUtils::createBuffer(this->device, this->physicalDevice, bufferSize, 171 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 172 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 173 storageBufferSet.buffers[i], storageBufferSet.memory[i]); 174 175 storageBufferSet.infoSet[i].buffer = storageBufferSet.buffers[i]; 176 storageBufferSet.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 177 storageBufferSet.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 178 } 179 } 180 } 181 182 // TODO: Move as much cleanup as I can into the destructor 137 183 template<class VertexType, class SSBOType> 138 184 GraphicsPipeline_Vulkan<VertexType, SSBOType>::~GraphicsPipeline_Vulkan() { … … 162 208 163 209 template<class VertexType, class SSBOType> 164 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) { 210 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addStorageDescriptor() { 211 if (!is_same_v<SSBOType, void*>) { 212 addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 213 VK_SHADER_STAGE_VERTEX_BIT, &storageBufferSet.infoSet); 214 } 215 } 216 217 template<class VertexType, class SSBOType> 218 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, 219 VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) { 165 220 this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr }); 166 221 } 167 222 168 223 template<class VertexType, class SSBOType> 169 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) { 224 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, 225 VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) { 170 226 this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData }); 171 227 } … … 351 407 } 352 408 409 // TODO: Since I only need the size of the swapChainImages array, I should just pass that in instead of the whole array 353 410 template<class VertexType, class SSBOType> 354 411 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSets(vector<VkImage>& swapChainImages) { … … 398 455 399 456 template<class VertexType, class SSBOType> 400 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) { 457 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer, 458 uint32_t currentImage) { 401 459 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); 402 460 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, … … 436 494 vkDestroyPipeline(device, pipeline, nullptr); 437 495 vkDestroyDescriptorPool(device, descriptorPool, nullptr); 496 497 // TODO: I read that the pipeline layout does not have to be recreated every time 498 // Try only creating it once 438 499 vkDestroyPipelineLayout(device, pipelineLayout, nullptr); 439 500 } … … 447 508 vkDestroyBuffer(device, indexBuffer, nullptr); 448 509 vkFreeMemory(device, indexBufferMemory, nullptr); 510 511 if (!is_same_v<SSBOType, void*>) { 512 for (size_t i = 0; i < storageBufferSet.buffers.size(); i++) { 513 vkDestroyBuffer(device, storageBufferSet.buffers[i], nullptr); 514 vkFreeMemory(device, storageBufferSet.memory[i], nullptr); 515 } 516 } 449 517 } 450 518 … … 486 554 487 555 template<class VertexType, class SSBOType> 488 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { 556 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool, 557 VkQueue graphicsQueue) { 489 558 VkBuffer newVertexBuffer; 490 559 VkDeviceMemory newVertexBufferMemory; 491 vertexCapacity *= 2;492 493 VulkanUtils::createBuffer( device, physicalDevice,vertexCapacity * sizeof(VertexType),560 this->vertexCapacity *= 2; 561 562 VulkanUtils::createBuffer(this->device, this->physicalDevice, this->vertexCapacity * sizeof(VertexType), 494 563 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 495 564 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newVertexBuffer, newVertexBufferMemory); 496 565 497 VulkanUtils::copyBuffer( device, commandPool, vertexBuffer, newVertexBuffer, 0, 0, numVertices * sizeof(VertexType), graphicsQueue);498 499 vkDestroyBuffer( device, vertexBuffer, nullptr);500 vkFreeMemory( device, vertexBufferMemory, nullptr);566 VulkanUtils::copyBuffer(this->device, commandPool, vertexBuffer, newVertexBuffer, 0, 0, numVertices * sizeof(VertexType), graphicsQueue); 567 568 vkDestroyBuffer(this->device, vertexBuffer, nullptr); 569 vkFreeMemory(this->device, vertexBufferMemory, nullptr); 501 570 502 571 vertexBuffer = newVertexBuffer; … … 505 574 506 575 template<class VertexType, class SSBOType> 507 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { 576 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool, 577 VkQueue graphicsQueue) { 508 578 VkBuffer newIndexBuffer; 509 579 VkDeviceMemory newIndexBufferMemory; 510 indexCapacity *= 2;511 512 VulkanUtils::createBuffer( device, physicalDevice,indexCapacity * sizeof(uint16_t),580 this->indexCapacity *= 2; 581 582 VulkanUtils::createBuffer(this->device, this->physicalDevice, this->indexCapacity * sizeof(uint16_t), 513 583 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 514 584 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newIndexBuffer, newIndexBufferMemory); 515 585 516 VulkanUtils::copyBuffer( device, commandPool, indexBuffer, newIndexBuffer, 0, 0, numIndices * sizeof(uint16_t), graphicsQueue);517 518 vkDestroyBuffer( device, indexBuffer, nullptr);519 vkFreeMemory( device, indexBufferMemory, nullptr);586 VulkanUtils::copyBuffer(this->device, commandPool, indexBuffer, newIndexBuffer, 0, 0, numIndices * sizeof(uint16_t), graphicsQueue); 587 588 vkDestroyBuffer(this->device, indexBuffer, nullptr); 589 vkFreeMemory(this->device, indexBufferMemory, nullptr); 520 590 521 591 indexBuffer = newIndexBuffer;
Note:
See TracChangeset
for help on using the changeset viewer.