Changeset a3cefaa in opengl-game for graphics-pipeline_vulkan.hpp


Ignore:
Timestamp:
May 17, 2021, 4:06:33 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
1abebc1
Parents:
996dd3e
Message:

Move SSBO resizing and pipeline recreation checks out of addObject() and into updateScene() so that those operations are only done at most once per pipeline per frame, using vkUpdateDescriptorSets() instead of recreating the whole graphics pipeline, and create a VulkanBuffer class for managing data related to uniform buffers and shader storage buffers, move objectCapacity and numObjects out of GraphicsPipeline_vulkan and use VulkanBuffer to manage them instead

File:
1 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    r996dd3e ra3cefaa  
    3636   public:
    3737      string vertShaderFile, fragShaderFile;
    38 
    39       // Both of these are only used for managing the SSBO, so move them out as well
    40       size_t objectCapacity;
    41       size_t numObjects;
    4238
    4339      GraphicsPipeline_Vulkan();
     
    4844      GraphicsPipeline_Vulkan(VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device,
    4945         VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages,
    50          size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity);
     46         size_t vertexCapacity, size_t indexCapacity);
    5147      ~GraphicsPipeline_Vulkan();
    5248
     
    6460      void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData);
    6561
    66       void updateDescriptorInfo(uint32_t index, vector<VkDescriptorBufferInfo>* bufferData);
     62      void updateDescriptorInfo(uint32_t index, vector<VkDescriptorBufferInfo>* bufferData,
     63                                vector<VkImage>& swapChainImages);
    6764      // TODO: Maybe make an analogous one for updating image info
    6865
     
    128125// into the constructor. That way, I can also put relevant cleanup code into the destructor
    129126template<class VertexType>
    130 GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan(
    131       VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device,
    132       VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages,
    133       size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity) {
    134    this->topology = topology;
    135    this->physicalDevice = physicalDevice;
    136    this->device = device;
    137    this->renderPass = renderPass;
     127GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan(VkPrimitiveTopology topology,
     128                                                             VkPhysicalDevice physicalDevice, VkDevice device,
     129                                                             VkRenderPass renderPass, Viewport viewport,
     130                                                             vector<VkImage>& swapChainImages, size_t vertexCapacity,
     131                                                             size_t indexCapacity)
     132                                                            : topology(topology)
     133                                                            , physicalDevice(physicalDevice)
     134                                                            , device(device)
     135                                                            , renderPass(renderPass) {
     136   // This is a member of the base GraphicsPipeline class
     137   // It currently is not used for the OpenGL pipeline. Either figure out why (since OpenGL certainly has the concept of
     138   // viewports) and use it there too and add viewport the the base class constructor, or create a second base class
     139   // constructor which takes the viewport
    138140   this->viewport = viewport;
    139141
     
    158160      VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
    159161      VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
    160 
    161    this->numObjects = 0;
    162    this->objectCapacity = objectCapacity;
    163162}
    164163
     
    204203template<class VertexType>
    205204void GraphicsPipeline_Vulkan<VertexType>::updateDescriptorInfo(uint32_t index,
    206                                                                          vector<VkDescriptorBufferInfo>* bufferData) {
     205                                                               vector<VkDescriptorBufferInfo>* bufferData,
     206                                                               vector<VkImage>& swapChainImages) {
    207207   this->descriptorInfoList[index].bufferDataList = bufferData;
     208
     209   // TODO: This code was mostly copied from createDescriptorSets. I should make some common function they both use
     210   // for updating descriptor sets
     211   for (size_t i = 0; i < swapChainImages.size(); i++) {
     212      VkWriteDescriptorSet descriptorWrite = {};
     213
     214      descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
     215      descriptorWrite.dstSet = this->descriptorSets[i];
     216      descriptorWrite.dstBinding = index;
     217      descriptorWrite.dstArrayElement = 0;
     218      descriptorWrite.descriptorType = this->descriptorInfoList[index].type;
     219      descriptorWrite.descriptorCount = 1;
     220      descriptorWrite.pBufferInfo = nullptr;
     221      descriptorWrite.pImageInfo = nullptr;
     222      descriptorWrite.pTexelBufferView = nullptr;
     223
     224      // This method is only intended for updated descriptor sets of type VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
     225      // but I'm leaving that in here for completeness
     226      switch (descriptorWrite.descriptorType) {
     227         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
     228         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
     229         case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
     230            descriptorWrite.pBufferInfo = &(*this->descriptorInfoList[index].bufferDataList)[i];
     231            break;
     232         case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
     233            descriptorWrite.pImageInfo = this->descriptorInfoList[index].imageData;
     234            break;
     235         default:
     236            throw runtime_error("Unknown descriptor type: " + to_string(descriptorWrite.descriptorType));
     237      }
     238
     239      if (bufferData->size() != swapChainImages.size()) {
     240         cout << "ALERT ALERT ALERT: SIZE MISMATCH!!!!!!!" << endl;
     241      }
     242
     243      vkUpdateDescriptorSets(this->device, 1, &descriptorWrite, 0, nullptr);
     244   }
    208245}
    209246
Note: See TracChangeset for help on using the changeset viewer.