[a3cefaa] | 1 | #ifndef _VULKAN_BUFFER_H
|
---|
| 2 | #define _VULKAN_BUFFER_H
|
---|
| 3 |
|
---|
| 4 | /*
|
---|
| 5 | * This class is intended to be used with Storage Buffers and Uniform Buffers.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | template<class T>
|
---|
| 9 | class VulkanBuffer {
|
---|
| 10 |
|
---|
| 11 | public:
|
---|
| 12 |
|
---|
[8dcbf62] | 13 | // TODO: Make these private (maybe make a getter for numObjects)
|
---|
| 14 | // Externally, they are only used in resizeBufferSet
|
---|
[a3cefaa] | 15 | size_t capacity;
|
---|
[6bac215] | 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;
|
---|
[a3cefaa] | 21 |
|
---|
| 22 | VulkanBuffer();
|
---|
| 23 | VulkanBuffer(size_t capacity, size_t minOffsetAlignment);
|
---|
[6bac215] | 24 |
|
---|
| 25 | VulkanBuffer(const VulkanBuffer<T>&) = delete;
|
---|
| 26 | VulkanBuffer(VulkanBuffer<T>&& other);
|
---|
| 27 |
|
---|
[a3cefaa] | 28 | ~VulkanBuffer();
|
---|
| 29 |
|
---|
[6bac215] | 30 | VulkanBuffer<T>& operator=(const VulkanBuffer<T>&) = delete;
|
---|
| 31 | VulkanBuffer<T>& operator=(VulkanBuffer<T>&& other) noexcept;
|
---|
| 32 |
|
---|
| 33 | void resize();
|
---|
[a3cefaa] | 34 |
|
---|
[8dcbf62] | 35 | void add(T obj);
|
---|
[a3cefaa] | 36 |
|
---|
[1abebc1] | 37 | // TODO: Add a resize function
|
---|
| 38 |
|
---|
[a3cefaa] | 39 | private:
|
---|
| 40 |
|
---|
[8dcbf62] | 41 | size_t alignment;
|
---|
[6bac215] | 42 | size_t numObjects;
|
---|
[8dcbf62] | 43 |
|
---|
[a3cefaa] | 44 | T* srcData; // TODO: Rename this to something else probably and rename rawData to data
|
---|
| 45 | vector<T>* vData;
|
---|
| 46 |
|
---|
| 47 | // Remember that this is a pointer to the mapped video memory
|
---|
| 48 | // Maybe rename it to mappedData or something to make that clearer
|
---|
| 49 | void* rawData;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | // Currently, for SSBOs, I store the per-object values (usually just the model matrix), on each object, so they
|
---|
| 53 | // are not in their own array and therefore cannot be pushed to the GPU as one block. The updates must happen
|
---|
| 54 | // separately per object.
|
---|
| 55 |
|
---|
| 56 | // Since Sascha WIllems' dynamic UBO example works the same way (iirc), I can implement dynamic UBOs like that as well
|
---|
| 57 | // for now. Would be nice to plan for potentially storing the ubo data on the CPU in a contiguous block in the future,
|
---|
| 58 | // assuming that would make updates easier. Keep in mind that this only makes sense if all or most of the objects
|
---|
| 59 | // in the ubo get updated every frame.
|
---|
| 60 |
|
---|
| 61 | // ============================= TODO: Also, check when it makes sense to have a staging buffer for copying data to the GPU
|
---|
| 62 | // and see if I actually need to use it everywhere I currently am. I think this is mentioned in Sascha WIllems dubo example
|
---|
| 63 | // or some other Vulkan website I recently bookmarked
|
---|
| 64 |
|
---|
| 65 | template<class T>
|
---|
| 66 | VulkanBuffer<T>::VulkanBuffer()
|
---|
| 67 | : alignment(0)
|
---|
| 68 | , capacity(0)
|
---|
| 69 | , numObjects(0)
|
---|
[6bac215] | 70 | , resized(false)
|
---|
[a3cefaa] | 71 | , srcData(nullptr)
|
---|
| 72 | , rawData(nullptr)
|
---|
| 73 | , vData(nullptr) {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | template<class T>
|
---|
| 77 | VulkanBuffer<T>::VulkanBuffer(size_t capacity, size_t minOffsetAlignment)
|
---|
| 78 | : alignment(sizeof(T))
|
---|
| 79 | , capacity(capacity)
|
---|
| 80 | , numObjects(0)
|
---|
[6bac215] | 81 | , resized(false)
|
---|
[a3cefaa] | 82 | , srcData(nullptr)
|
---|
| 83 | , rawData(nullptr)
|
---|
| 84 | , vData(nullptr) {
|
---|
| 85 | if (minOffsetAlignment > 0) {
|
---|
| 86 | alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | srcData = (T*)malloc(capacity * alignment);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[6bac215] | 92 | template<class T>
|
---|
| 93 | VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) {
|
---|
| 94 | // TODO: Implement
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[a3cefaa] | 97 | template<class T>
|
---|
| 98 | VulkanBuffer<T>::~VulkanBuffer() {
|
---|
| 99 | if (srcData != nullptr) {
|
---|
| 100 | free(srcData);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | template<class T>
|
---|
[6bac215] | 105 | VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept {
|
---|
| 106 | if (this != &other) {
|
---|
| 107 | capacity = other.capacity;
|
---|
| 108 | numObjects = other.numObjects;
|
---|
| 109 | resized = other.resized;
|
---|
[a3cefaa] | 110 |
|
---|
[6bac215] | 111 | alignment = other.alignment;
|
---|
[a3cefaa] | 112 |
|
---|
[6bac215] | 113 | if (srcData != nullptr) {
|
---|
| 114 | free(srcData);
|
---|
| 115 | }
|
---|
[a3cefaa] | 116 |
|
---|
[6bac215] | 117 | srcData = other.srcData;
|
---|
[a3cefaa] | 118 |
|
---|
[6bac215] | 119 | other.capacity = 0;
|
---|
| 120 | other.numObjects = 0;
|
---|
| 121 | // TODO: Maybe set rnage to 0 as well
|
---|
[a3cefaa] | 122 |
|
---|
[6bac215] | 123 | other.srcData = nullptr;
|
---|
| 124 | }
|
---|
[a3cefaa] | 125 |
|
---|
| 126 | return *this;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[6bac215] | 129 | template<class T>
|
---|
| 130 | void VulkanBuffer<T>::resize() {
|
---|
| 131 | resized = false;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[a3cefaa] | 134 | template<class T>
|
---|
[8dcbf62] | 135 | void VulkanBuffer<T>::add(T obj) {
|
---|
[6bac215] | 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 |
|
---|
[8dcbf62] | 143 | numObjects++;
|
---|
[a3cefaa] | 144 | }
|
---|
| 145 |
|
---|
| 146 | #endif // _VULKAN_BUFFER_H
|
---|