Changeset 860a0da in opengl-game for graphics-pipeline_vulkan.hpp


Ignore:
Timestamp:
Feb 18, 2020, 9:36:51 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
2da64ef
Parents:
d25381b
Message:

In VulkanGame, move fields related to the ssbo, as well as code to create thee ssbo, destroy it, and create a descriptor for it, into GraphicsPipeline_Vulkan

File:
1 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    rd25381b r860a0da  
    55
    66#include <fstream>
     7#include <iostream>
    78#include <stdexcept>
    89#include <vector>
     
    3132};
    3233
     34struct StorageBufferSet {
     35   vector<VkBuffer> buffers;
     36   vector<VkDeviceMemory> memory;
     37   vector<VkDescriptorBufferInfo> infoSet;
     38};
     39
    3340template<class VertexType, class SSBOType>
    3441class GraphicsPipeline_Vulkan : public GraphicsPipeline {
    3542   public:
    3643      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
    3748      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);
    3951      ~GraphicsPipeline_Vulkan();
    4052
     
    4658
    4759      void addAttribute(VkFormat format, size_t offset);
     60
     61      void addStorageDescriptor();
    4862
    4963      void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData);
     
    6276      void cleanup();
    6377      void cleanupBuffers();
     78
     79      StorageBufferSet storageBufferSet;
    6480   
    6581   private:
     
    90106      VkDeviceMemory indexBufferMemory;
    91107
     108      size_t numObjects;
     109      size_t objectCapacity;
     110
    92111      VkShaderModule createShaderModule(const vector<char>& code);
    93112      vector<char> readFile(const string& filename);
     
    107126GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan(
    108127      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) {
    110130   this->physicalDevice = physicalDevice;
    111131   this->device = device;
     
    133153      VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
    134154      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
    137183template<class VertexType, class SSBOType>
    138184GraphicsPipeline_Vulkan<VertexType, SSBOType>::~GraphicsPipeline_Vulkan() {
     
    162208
    163209template<class VertexType, class SSBOType>
    164 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
     210void 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
     217template<class VertexType, class SSBOType>
     218void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type,
     219      VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
    165220   this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr });
    166221}
    167222
    168223template<class VertexType, class SSBOType>
    169 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
     224void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type,
     225      VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
    170226   this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData });
    171227}
     
    351407}
    352408
     409// TODO: Since I only need the size of the swapChainImages array, I should just pass that in instead of the whole array
    353410template<class VertexType, class SSBOType>
    354411void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSets(vector<VkImage>& swapChainImages) {
     
    398455
    399456template<class VertexType, class SSBOType>
    400 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {
     457void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer,
     458      uint32_t currentImage) {
    401459   vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
    402460   vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1,
     
    436494   vkDestroyPipeline(device, pipeline, nullptr);
    437495   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
    438499   vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
    439500}
     
    447508   vkDestroyBuffer(device, indexBuffer, nullptr);
    448509   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   }
    449517}
    450518
     
    486554
    487555template<class VertexType, class SSBOType>
    488 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
     556void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool,
     557      VkQueue graphicsQueue) {
    489558   VkBuffer newVertexBuffer;
    490559   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),
    494563      VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
    495564      VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newVertexBuffer, newVertexBufferMemory);
    496565
    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);
    501570
    502571   vertexBuffer = newVertexBuffer;
     
    505574
    506575template<class VertexType, class SSBOType>
    507 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
     576void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool,
     577      VkQueue graphicsQueue) {
    508578   VkBuffer newIndexBuffer;
    509579   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),
    513583      VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
    514584      VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newIndexBuffer, newIndexBufferMemory);
    515585
    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);
    520590
    521591   indexBuffer = newIndexBuffer;
Note: See TracChangeset for help on using the changeset viewer.