Changeset 9d21aac in opengl-game for vulkan-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
  • vulkan-game.hpp

    r756162f r9d21aac  
    142142template<class VertexType, class SSBOType>
    143143struct EffectOverTime : public BaseEffectOverTime {
    144    GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline;
     144   GraphicsPipeline_Vulkan<VertexType>& pipeline;
    145145   vector<SceneObject<VertexType, SSBOType>>& objects;
    146146   unsigned int objectIndex;
     
    150150   float changePerSecond;
    151151
    152    EffectOverTime(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    153          vector<SceneObject<VertexType, SSBOType>>& objects, unsigned int objectIndex,
    154          size_t effectedFieldOffset, float startTime, float changePerSecond) :
    155          pipeline(pipeline),
    156          objects(objects),
    157          objectIndex(objectIndex),
    158          effectedFieldOffset(effectedFieldOffset),
    159          startTime(startTime),
    160          changePerSecond(changePerSecond) {
     152   EffectOverTime(GraphicsPipeline_Vulkan<VertexType>& pipeline, vector<SceneObject<VertexType, SSBOType>>& objects,
     153                  unsigned int objectIndex, size_t effectedFieldOffset, float startTime, float changePerSecond)
     154               : pipeline(pipeline)
     155               , objects(objects)
     156               , objectIndex(objectIndex)
     157               , effectedFieldOffset(effectedFieldOffset)
     158               , startTime(startTime)
     159               , changePerSecond(changePerSecond) {
    161160      size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
    162161
     
    301300      // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
    302301      // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
    303       GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
    304       GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> shipPipeline;
    305       GraphicsPipeline_Vulkan<ModelVertex, SSBO_Asteroid> asteroidPipeline;
    306       GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser> laserPipeline;
    307       GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline;
     302      GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
     303      GraphicsPipeline_Vulkan<ModelVertex> shipPipeline;
     304      GraphicsPipeline_Vulkan<ModelVertex> asteroidPipeline;
     305      GraphicsPipeline_Vulkan<LaserVertex> laserPipeline;
     306      GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline;
    308307
    309308      // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
     
    420419      void cleanupImGuiOverlay();
    421420
    422       void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
    423          vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
    424          vector<VkDescriptorBufferInfo>& bufferInfoList);
     421      // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
     422
     423      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties,
     424                           vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     425                           vector<VkDescriptorBufferInfo>& bufferInfoList);
     426
     427      // TODO: See if it makes sense to rename this to resizeBufferSet() and use it to resize other types of buffers as well
     428      // TODO: Remove the need for templating, which is only there so a GraphicsPupeline_Vulkan can be passed in
     429      template<class VertexType, class SSBOType>
     430      void resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue,
     431                                  GraphicsPipeline_Vulkan<VertexType>& pipeline);
     432
     433      template<class SSBOType>
     434      void updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo);
    425435
    426436      // TODO: Since addObject() returns a reference to the new object now,
    427437      // stop using objects.back() to access the object that was just created
    428438      template<class VertexType, class SSBOType>
    429       SceneObject<VertexType, SSBOType>& addObject(
    430             vector<SceneObject<VertexType, SSBOType>>& objects,
    431             GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    432             const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
    433             bool pipelinesCreated);
     439      SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     440                                                   GraphicsPipeline_Vulkan<VertexType>& pipeline,
     441                                                   const vector<VertexType>& vertices, vector<uint16_t> indices,
     442                                                   SSBOType ssbo, bool pipelinesCreated);
    434443
    435444      template<class VertexType>
     
    444453      template<class VertexType, class SSBOType>
    445454      void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    446             GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
     455            GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index);
    447456
    448457      template<class VertexType, class SSBOType>
    449       void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     458      void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
    450459            SceneObject<VertexType, SSBOType>& obj, size_t index);
    451460
     
    485494// End of specialized no-op functions
    486495
     496template<class VertexType, class SSBOType>
     497void VulkanGame::resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue,
     498                                        GraphicsPipeline_Vulkan<VertexType>& pipeline) {
     499   pipeline.objectCapacity *= 2;
     500   VkDeviceSize bufferSize = pipeline.objectCapacity * sizeof(SSBOType);
     501
     502   for (size_t i = 0; i < set.buffers.size(); i++) {
     503      VkBuffer newStorageBuffer;
     504      VkDeviceMemory newStorageBufferMemory;
     505
     506      VulkanUtils::createBuffer(device, physicalDevice, bufferSize,
     507         VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     508         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     509         newStorageBuffer, newStorageBufferMemory);
     510
     511      VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer,
     512         0, 0, pipeline.numObjects * sizeof(SSBOType), graphicsQueue);
     513
     514      vkDestroyBuffer(device, set.buffers[i], nullptr);
     515      vkFreeMemory(device, set.memory[i], nullptr);
     516
     517      set.buffers[i] = newStorageBuffer;
     518      set.memory[i] = newStorageBufferMemory;
     519
     520      set.infoSet[i].buffer = set.buffers[i];
     521      set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
     522      set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
     523   }
     524}
     525
     526// TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
     527template<class SSBOType>
     528void VulkanGame::updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo) {
     529   for (size_t i = 0; i < storageBufferSet.memory.size(); i++) {
     530      VulkanUtils::copyDataToMemory(device, ssbo, storageBufferSet.memory[i], objIndex * sizeof(SSBOType));
     531   }
     532}
     533
    487534// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
    488535// and to change the model matrix later by setting model_transform and then calling updateObject()
    489 // Figure out a better way to allow the model matrix to be set during objecting creation
     536// Figure out a better way to allow the model matrix to be set during object creation
    490537
    491538// TODO: Maybe return a reference to the object from this method if I decide that updating it
     
    494541// to account for scaling
    495542template<class VertexType, class SSBOType>
    496 SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
    497       vector<SceneObject<VertexType, SSBOType>>& objects,
    498       GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    499       const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
    500       bool pipelinesCreated) {
     543SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     544                                                         GraphicsPipeline_Vulkan<VertexType>& pipeline,
     545                                                         const vector<VertexType>& vertices, vector<uint16_t> indices,
     546                                                         SSBOType ssbo, bool pipelinesCreated) {
    501547   // TODO: Use the model field of ssbo to set the object's model_base
    502548   // currently, the passed in model is useless since it gets overridden in updateObject() anyway
     
    518564   }
    519565
    520    bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
    521       resourceCommandPool, graphicsQueue);
     566   pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
     567
     568   bool resizeStorageBuffer = pipeline.numObjects == pipeline.objectCapacity;
     569
     570   if (resizeStorageBuffer) {
     571      resizeStorageBufferSet<VertexType, SSBOType>(pipeline.storageBufferSet, resourceCommandPool, graphicsQueue, pipeline);
     572      pipeline.cleanup();
     573
     574      // Assume the SSBO is always the 2nd binding
     575      pipeline.updateDescriptorInfo(1, &pipeline.storageBufferSet.infoSet);
     576   }
     577
     578   pipeline.numObjects++;
     579
     580   updateStorageBuffer(pipeline.storageBufferSet, pipeline.numObjects - 1, obj.ssbo);
     581
     582   // TODO: Figure out why I am destroying and recreating the ubos when the swap chain is recreated,
     583   // but am reusing the same ssbos. Maybe I don't need to recreate the ubos.
    522584
    523585   if (pipelinesCreated) {
     
    532594      // Refactor the logic to check for any resized SSBOs after all objects for the frame
    533595      // are created and then recreate each of the corresponding pipelines only once per frame
    534       if (storageBufferResized) {
     596
     597      // TODO: Also, verify if I actually need to recreate all of these, or maybe just the descriptor sets, for instance
     598
     599      if (resizeStorageBuffer) {
    535600         pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
    536601         pipeline.createDescriptorPool(swapChainImages);
     
    628693template<class VertexType, class SSBOType>
    629694void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    630       GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
     695      GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index) {
    631696   SceneObject<VertexType, SSBOType>& obj = objects[index];
    632697
     
    634699   obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
    635700
    636    pipeline.updateObject(index, obj.ssbo);
     701   updateStorageBuffer(pipeline.storageBufferSet, index, obj.ssbo);
    637702
    638703   obj.modified = false;
     
    640705
    641706template<class VertexType, class SSBOType>
    642 void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     707void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
    643708      SceneObject<VertexType, SSBOType>& obj, size_t index) {
    644709   pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
Note: See TracChangeset for help on using the changeset viewer.