Changeset 9d21aac in opengl-game for sdl-game.hpp


Ignore:
Timestamp:
May 6, 2021, 3:24:42 AM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
996dd3e
Parents:
756162f
Message:

Remove the SSBOType template parameter from GraphicsPipeline_Vulkan

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.hpp

    r756162f r9d21aac  
    207207      // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
    208208      // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
    209       GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
     209      GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
    210210
    211211      // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
     
    276276      void cleanupImGuiOverlay();
    277277
    278       void createBufferSet(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, VkDeviceSize bufferSize,
    279                            VkBufferUsageFlags flags, VkMemoryPropertyFlags properties,
     278      // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
     279
     280      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties,
     281                           vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
    280282                           vector<VkDescriptorBufferInfo>& bufferInfoList);
     283
     284      // TODO: See if it makes sense to rename this to resizeBufferSet() and use it to resize other types of buffers as well
     285      // TODO: Remove the need for templating, which is only there so a GraphicsPupeline_Vulkan can be passed in
     286      template<class VertexType, class SSBOType>
     287      void resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue,
     288                                  GraphicsPipeline_Vulkan<VertexType>& pipeline);
     289
     290      template<class SSBOType>
     291      void updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo);
    281292
    282293      // TODO: Since addObject() returns a reference to the new object now,
     
    284295      template<class VertexType, class SSBOType>
    285296      SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    286                                                    GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     297                                                   GraphicsPipeline_Vulkan<VertexType>& pipeline,
    287298                                                   const vector<VertexType>& vertices, vector<uint16_t> indices,
    288299                                                   SSBOType ssbo, bool pipelinesCreated);
     
    298309
    299310      template<class VertexType, class SSBOType>
    300       void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, GraphicsPipeline_Vulkan<VertexType,
    301                         SSBOType>& pipeline, size_t index);
     311      void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     312                        GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index);
    302313
    303314      void renderFrame(ImDrawData* draw_data);
     
    320331};
    321332
     333template<class VertexType, class SSBOType>
     334void VulkanGame::resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue,
     335                                        GraphicsPipeline_Vulkan<VertexType>& pipeline) {
     336   pipeline.objectCapacity *= 2;
     337   VkDeviceSize bufferSize = pipeline.objectCapacity * sizeof(SSBOType);
     338
     339   for (size_t i = 0; i < set.buffers.size(); i++) {
     340      VkBuffer newStorageBuffer;
     341      VkDeviceMemory newStorageBufferMemory;
     342
     343      VulkanUtils::createBuffer(device, physicalDevice, bufferSize,
     344         VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     345         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     346         newStorageBuffer, newStorageBufferMemory);
     347
     348      VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer,
     349         0, 0, pipeline.numObjects * sizeof(SSBOType), graphicsQueue);
     350
     351      vkDestroyBuffer(device, set.buffers[i], nullptr);
     352      vkFreeMemory(device, set.memory[i], nullptr);
     353
     354      set.buffers[i] = newStorageBuffer;
     355      set.memory[i] = newStorageBufferMemory;
     356
     357      set.infoSet[i].buffer = set.buffers[i];
     358      set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
     359      set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
     360   }
     361}
     362
     363// TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
     364template<class SSBOType>
     365void VulkanGame::updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo) {
     366   for (size_t i = 0; i < storageBufferSet.memory.size(); i++) {
     367      VulkanUtils::copyDataToMemory(device, ssbo, storageBufferSet.memory[i], objIndex * sizeof(SSBOType));
     368   }
     369}
     370
    322371// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
    323372// and to change the model matrix later by setting model_transform and then calling updateObject()
    324 // Figure out a better way to allow the model matrix to be set during objecting creation
     373// Figure out a better way to allow the model matrix to be set during object creation
    325374
    326375// TODO: Maybe return a reference to the object from this method if I decide that updating it
     
    329378// to account for scaling
    330379template<class VertexType, class SSBOType>
    331 SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
    332    vector<SceneObject<VertexType, SSBOType>>& objects,
    333    GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    334    const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
    335    bool pipelinesCreated) {
     380SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     381                                                         GraphicsPipeline_Vulkan<VertexType>& pipeline,
     382                                                         const vector<VertexType>& vertices, vector<uint16_t> indices,
     383                                                         SSBOType ssbo, bool pipelinesCreated) {
    336384   // TODO: Use the model field of ssbo to set the object's model_base
    337385   // currently, the passed in model is useless since it gets overridden in updateObject() anyway
     
    353401   }
    354402
    355    bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
    356       resourceCommandPool, graphicsQueue);
     403   pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
     404
     405   bool resizeStorageBuffer = pipeline.numObjects == pipeline.objectCapacity;
     406
     407   if (resizeStorageBuffer) {
     408      resizeStorageBufferSet<VertexType, SSBOType>(pipeline.storageBufferSet, resourceCommandPool, graphicsQueue, pipeline);
     409      pipeline.cleanup();
     410
     411      // Assume the SSBO is always the 2nd binding
     412      pipeline.updateDescriptorInfo(1, &pipeline.storageBufferSet.infoSet);
     413   }
     414
     415   pipeline.numObjects++;
     416
     417   updateStorageBuffer(pipeline.storageBufferSet, pipeline.numObjects - 1, obj.ssbo);
     418
     419   // TODO: Figure out why I am destroying and recreating the ubos when the swap chain is recreated,
     420   // but am reusing the same ssbos. Maybe I don't need to recreate the ubos.
    357421
    358422   if (pipelinesCreated) {
     
    367431      // Refactor the logic to check for any resized SSBOs after all objects for the frame
    368432      // are created and then recreate each of the corresponding pipelines only once per frame
    369       if (storageBufferResized) {
     433
     434      // TODO: Also, verify if I actually need to recreate all of these, or maybe just the descriptor sets, for instance
     435
     436      if (resizeStorageBuffer) {
    370437         pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
    371438         pipeline.createDescriptorPool(swapChainImages);
     
    457524// TODO: Just pass in the single object instead of a list of all of them
    458525template<class VertexType, class SSBOType>
    459 void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, GraphicsPipeline_Vulkan<VertexType,
    460                               SSBOType>& pipeline, size_t index) {
     526void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     527                              GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index) {
    461528   SceneObject<VertexType, SSBOType>& obj = objects[index];
    462529
     
    464531   obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
    465532
    466    pipeline.updateObject(index, obj.ssbo);
     533   updateStorageBuffer(pipeline.storageBufferSet, index, obj.ssbo);
    467534
    468535   obj.modified = false;
Note: See TracChangeset for help on using the changeset viewer.