Changeset 6bac215 in opengl-game


Ignore:
Timestamp:
Jun 9, 2021, 12:38:14 AM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
bb76950
Parents:
8dcbf62
git-author:
Dmitry Portnoy <dportnoy@…> (06/09/21 00:38:09)
git-committer:
Dmitry Portnoy <dportnoy@…> (06/09/21 00:38:14)
Message:

Rewrite a large portion of the VulkanBuffer class, start using it more
to resize buffers, and simplify resizeBufferSet() since the additions to
VulkanBuffer can now do some of what that function used to do

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.cpp

    r8dcbf62 r6bac215  
    269269
    270270   createBufferSet(objects_modelPipeline.capacity * sizeof(SSBO_ModelObject),
    271       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    272       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    273       storageBuffers_modelPipeline);
     271                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     272                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     273                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     274                   storageBuffers_modelPipeline);
    274275}
    275276
     
    456457
    457458   // TODO: Probably move the resizing to the VulkanBuffer class
    458    if (objects_modelPipeline.numObjects > objects_modelPipeline.capacity) {
     459   if (objects_modelPipeline.resized) {
    459460      // TODO: Also resize the dynamic ubo
    460461      resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline, modelPipeline, resourceCommandPool,
    461462                      graphicsQueue);
     463
     464      objects_modelPipeline.resize();
    462465   }
    463466
  • sdl-game.hpp

    r8dcbf62 r6bac215  
    348348                                 GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool,
    349349                                 VkQueue graphicsQueue) {
    350    size_t numObjects = buffer.numObjects < buffer.capacity ? buffer.numObjects : buffer.capacity;
    351 
    352    do {
    353       buffer.capacity *= 2;
    354    } while (buffer.capacity < buffer.numObjects);
    355 
    356    VkDeviceSize bufferSize = buffer.capacity * sizeof(SSBOType);
     350   VkDeviceSize newSize = buffer.capacity * sizeof(SSBOType);
    357351
    358352   for (size_t i = 0; i < set.buffers.size(); i++) {
    359       VkBuffer newStorageBuffer;
    360       VkDeviceMemory newStorageBufferMemory;
    361 
    362       VulkanUtils::createBuffer(device, physicalDevice, bufferSize,
    363          VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    364          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    365          newStorageBuffer, newStorageBufferMemory);
    366 
    367       VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer,
    368          0, 0, numObjects * sizeof(SSBOType), graphicsQueue);
     353      VkBuffer newBuffer;
     354      VkDeviceMemory newMemory;
     355
     356      VulkanUtils::createBuffer(device, physicalDevice, newSize, set.usages, set.properties, newBuffer, newMemory);
     357
     358      VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newBuffer, 0, 0, set.infoSet[i].range,
     359                              graphicsQueue);
    369360
    370361      vkDestroyBuffer(device, set.buffers[i], nullptr);
    371362      vkFreeMemory(device, set.memory[i], nullptr);
    372363
    373       set.buffers[i] = newStorageBuffer;
    374       set.memory[i] = newStorageBufferMemory;
     364      set.buffers[i] = newBuffer;
     365      set.memory[i] = newMemory;
    375366
    376367      set.infoSet[i].buffer = set.buffers[i];
    377368      set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
    378       set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
     369      set.infoSet[i].range = newSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
    379370   }
    380371
  • vulkan-buffer.hpp

    r8dcbf62 r6bac215  
    1414      // Externally, they are only used in resizeBufferSet
    1515      size_t capacity;
    16       size_t numObjects;
     16
     17      // temp field to help with ubo+ssbo resizing until they are added to this class
     18      // See if I need a separate field for this or if I can use other fields to check for this
     19      // Maybe compare uniform or storage buffer size to the size of the memory allocated here
     20      bool resized;
    1721
    1822      VulkanBuffer();
    1923      VulkanBuffer(size_t capacity, size_t minOffsetAlignment);
     24
     25      VulkanBuffer(const VulkanBuffer<T>&) = delete;
     26      VulkanBuffer(VulkanBuffer<T>&& other);
     27
    2028      ~VulkanBuffer();
    2129
    22       VulkanBuffer<T>& operator=(const VulkanBuffer<T>& other);
     30      VulkanBuffer<T>& operator=(const VulkanBuffer<T>&) = delete;
     31      VulkanBuffer<T>& operator=(VulkanBuffer<T>&& other) noexcept;
     32
     33      void resize();
    2334
    2435      void add(T obj);
     
    2940
    3041      size_t alignment;
     42      size_t numObjects;
    3143
    3244      T* srcData; // TODO: Rename this to something else probably and rename rawData to data
     
    5668                              , capacity(0)
    5769                              , numObjects(0)
     70                              , resized(false)
    5871                              , srcData(nullptr)
    5972                              , rawData(nullptr)
     
    6679                              , capacity(capacity)
    6780                              , numObjects(0)
     81                              , resized(false)
    6882                              , srcData(nullptr)
    6983                              , rawData(nullptr)
     
    7791
    7892template<class T>
     93VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) {
     94   // TODO: Implement
     95}
     96
     97template<class T>
    7998VulkanBuffer<T>::~VulkanBuffer() {
    8099   if (srcData != nullptr) {
     
    84103
    85104template<class T>
    86 VulkanBuffer<T>& VulkanBuffer<T>::operator=(const VulkanBuffer<T>& other) {
    87    if (this == &other) {
    88       return *this;
     105VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept {
     106   if (this != &other) {
     107      capacity = other.capacity;
     108      numObjects = other.numObjects;
     109      resized = other.resized;
     110
     111      alignment = other.alignment;
     112
     113      if (srcData != nullptr) {
     114         free(srcData);
     115      }
     116
     117      srcData = other.srcData;
     118
     119      other.capacity = 0;
     120      other.numObjects = 0;
     121      // TODO: Maybe set rnage to 0 as well
     122
     123      other.srcData = nullptr;
    89124   }
    90 
    91    /*
    92    // assume *this manages a reusable resource, such as a heap-allocated buffer mArray
    93    if (size != other.size) {        // resource in *this cannot be reused
    94       delete[] mArray;              // release resource in *this
    95       mArray = nullptr;
    96       size = 0;                     // preserve invariants in case next line throws
    97       mArray = new int[other.size]; // allocate resource in *this
    98       size = other.size;
    99    }
    100    */
    101 
    102    if (srcData != nullptr) {
    103       free(srcData);
    104       srcData = nullptr;
    105    }
    106 
    107    alignment = other.alignment;
    108    capacity = other.capacity;
    109 
    110    srcData = (T*)malloc(capacity * alignment);
    111    // TODO: Check for failure
    112 
    113    memcpy(srcData, other.srcData, capacity * alignment);
    114125
    115126   return *this;
     
    117128
    118129template<class T>
     130void VulkanBuffer<T>::resize() {
     131   resized = false;
     132}
     133
     134template<class T>
    119135void VulkanBuffer<T>::add(T obj) {
     136   if (numObjects == capacity) {
     137      // Once I add Vulkan buffer objects in here, make sure this doesn't overlap with resizeBufferSet
     138      resized = true;
     139
     140      capacity *= 2;
     141   }
     142
    120143   numObjects++;
    121144}
  • vulkan-game.cpp

    r8dcbf62 r6bac215  
    616616
    617617   createBufferSet(objects_modelPipeline.capacity * sizeof(SSBO_ModelObject),
    618       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    619       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    620       storageBuffers_modelPipeline);
     618                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     619                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     620                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     621                   storageBuffers_modelPipeline);
    621622
    622623   shipPipeline = GraphicsPipeline_Vulkan<ModelVertex>(
     
    625626
    626627   createBufferSet(objects_shipPipeline.capacity * sizeof(SSBO_ModelObject),
    627       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    628       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    629       storageBuffers_shipPipeline);
     628                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     629                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     630                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     631                   storageBuffers_shipPipeline);
    630632
    631633   asteroidPipeline = GraphicsPipeline_Vulkan<ModelVertex>(
     
    634636
    635637   createBufferSet(objects_asteroidPipeline.capacity * sizeof(SSBO_Asteroid),
    636       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    637       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    638       storageBuffers_asteroidPipeline);
     638                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     639                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     640                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     641                   storageBuffers_asteroidPipeline);
    639642
    640643   laserPipeline = GraphicsPipeline_Vulkan<LaserVertex>(
     
    643646
    644647   createBufferSet(objects_laserPipeline.capacity * sizeof(SSBO_Laser),
    645       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    646       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    647       storageBuffers_laserPipeline);
     648                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     649                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     650                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     651                   storageBuffers_laserPipeline);
    648652
    649653   explosionPipeline = GraphicsPipeline_Vulkan<ExplosionVertex>(
     
    653657
    654658   createBufferSet(objects_explosionPipeline.capacity * sizeof(SSBO_Explosion),
    655       VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    656       VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    657       storageBuffers_explosionPipeline);
     659                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
     660                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     661                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     662                   storageBuffers_explosionPipeline);
    658663}
    659664
     
    10851090
    10861091   // TODO: Probably move the resizing to the VulkanBuffer class
    1087    if (objects_modelPipeline.numObjects > objects_modelPipeline.capacity) {
     1092   if (objects_modelPipeline.resized) {
    10881093      resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline, modelPipeline, resourceCommandPool,
    10891094                      graphicsQueue);
     1095
     1096      objects_modelPipeline.resize();
    10901097   }
    10911098
     
    10981105
    10991106   // TODO: Probably move the resizing to the VulkanBuffer class
    1100    if (objects_shipPipeline.numObjects > objects_shipPipeline.capacity) {
     1107   if (objects_shipPipeline.resized) {
    11011108      resizeBufferSet(storageBuffers_shipPipeline, objects_shipPipeline, shipPipeline, resourceCommandPool,
    11021109                      graphicsQueue);
     1110
     1111      objects_shipPipeline.resize();
    11031112   }
    11041113
     
    11111120
    11121121   // TODO: Probably move the resizing to the VulkanBuffer class
    1113    if (objects_asteroidPipeline.numObjects > objects_asteroidPipeline.capacity) {
     1122   if (objects_asteroidPipeline.resized) {
    11141123      resizeBufferSet(storageBuffers_asteroidPipeline, objects_asteroidPipeline, asteroidPipeline,
    11151124                      resourceCommandPool, graphicsQueue);
     1125
     1126      objects_asteroidPipeline.resize();
    11161127   }
    11171128
     
    11241135
    11251136   // TODO: Probably move the resizing to the VulkanBuffer class
    1126    if (objects_laserPipeline.numObjects > objects_laserPipeline.capacity) {
     1137   if (objects_laserPipeline.resized) {
    11271138      resizeBufferSet(storageBuffers_laserPipeline, objects_laserPipeline, laserPipeline, resourceCommandPool,
    11281139                      graphicsQueue);
     1140
     1141      objects_laserPipeline.resize();
    11291142   }
    11301143
     
    11371150
    11381151   // TODO: Probably move the resizing to the VulkanBuffer class
    1139    if (objects_explosionPipeline.numObjects > objects_explosionPipeline.capacity) {
     1152   if (objects_explosionPipeline.resized) {
    11401153      resizeBufferSet(storageBuffers_explosionPipeline, objects_explosionPipeline, explosionPipeline,
    11411154                     resourceCommandPool, graphicsQueue);
     1155
     1156      objects_explosionPipeline.resize();
    11421157   }
    11431158
  • vulkan-game.hpp

    r8dcbf62 r6bac215  
    503503                                 GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool,
    504504                                 VkQueue graphicsQueue) {
    505    size_t numObjects = buffer.numObjects < buffer.capacity ? buffer.numObjects : buffer.capacity;
    506 
    507    do {
    508       buffer.capacity *= 2;
    509    } while (buffer.capacity < buffer.numObjects);
    510 
    511    VkDeviceSize bufferSize = buffer.capacity * sizeof(SSBOType);
     505   VkDeviceSize newSize = buffer.capacity * sizeof(SSBOType);
    512506
    513507   for (size_t i = 0; i < set.buffers.size(); i++) {
    514       VkBuffer newStorageBuffer;
    515       VkDeviceMemory newStorageBufferMemory;
    516 
    517       VulkanUtils::createBuffer(device, physicalDevice, bufferSize,
    518          VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    519          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    520          newStorageBuffer, newStorageBufferMemory);
    521 
    522       VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer,
    523          0, 0, numObjects * sizeof(SSBOType), graphicsQueue);
     508      VkBuffer newBuffer;
     509      VkDeviceMemory newMemory;
     510
     511      VulkanUtils::createBuffer(device, physicalDevice, newSize, set.usages, set.properties, newBuffer, newMemory);
     512
     513      VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newBuffer, 0, 0, set.infoSet[i].range,
     514                              graphicsQueue);
    524515
    525516      vkDestroyBuffer(device, set.buffers[i], nullptr);
    526517      vkFreeMemory(device, set.memory[i], nullptr);
    527518
    528       set.buffers[i] = newStorageBuffer;
    529       set.memory[i] = newStorageBufferMemory;
     519      set.buffers[i] = newBuffer;
     520      set.memory[i] = newMemory;
    530521
    531522      set.infoSet[i].buffer = set.buffers[i];
    532523      set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
    533       set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
     524      set.infoSet[i].range = newSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
    534525   }
    535526
Note: See TracChangeset for help on using the changeset viewer.