Changeset 6bac215 in opengl-game for vulkan-buffer.hpp


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.