[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();
|
---|
[b7fc3c2] | 23 | VulkanBuffer(size_t capacity, size_t range, 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 |
|
---|
[5ea0a37] | 33 | size_t memorySize();
|
---|
| 34 |
|
---|
| 35 | T* data(); // Not sure I need to expose this
|
---|
[a3cefaa] | 36 |
|
---|
[6486ba8] | 37 | T& get(uint32_t index);
|
---|
[8dcbf62] | 38 | void add(T obj);
|
---|
[a3cefaa] | 39 |
|
---|
[90880fb] | 40 | void* mapped(size_t idx);
|
---|
| 41 |
|
---|
| 42 | void map(vector<VkDeviceMemory>& deviceMemory, VkDevice device);
|
---|
| 43 | void unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device);
|
---|
| 44 |
|
---|
[5ea0a37] | 45 | void resize();
|
---|
[1abebc1] | 46 |
|
---|
[a3cefaa] | 47 | private:
|
---|
| 48 |
|
---|
[8dcbf62] | 49 | size_t alignment;
|
---|
[b7fc3c2] | 50 | size_t range;
|
---|
[6486ba8] | 51 | //size_t capacity;
|
---|
[6bac215] | 52 | size_t numObjects;
|
---|
[8dcbf62] | 53 |
|
---|
[6486ba8] | 54 | T* rawData;
|
---|
| 55 | vector<void*> mappedData;
|
---|
[b7fc3c2] | 56 |
|
---|
| 57 | size_t memRequirement(size_t capacity);
|
---|
[6486ba8] | 58 | size_t memOffset(uint32_t index);
|
---|
[a3cefaa] | 59 | };
|
---|
| 60 |
|
---|
| 61 | // Currently, for SSBOs, I store the per-object values (usually just the model matrix), on each object, so they
|
---|
| 62 | // are not in their own array and therefore cannot be pushed to the GPU as one block. The updates must happen
|
---|
| 63 | // separately per object.
|
---|
| 64 |
|
---|
| 65 | // Since Sascha WIllems' dynamic UBO example works the same way (iirc), I can implement dynamic UBOs like that as well
|
---|
| 66 | // for now. Would be nice to plan for potentially storing the ubo data on the CPU in a contiguous block in the future,
|
---|
| 67 | // assuming that would make updates easier. Keep in mind that this only makes sense if all or most of the objects
|
---|
| 68 | // in the ubo get updated every frame.
|
---|
| 69 |
|
---|
| 70 | // ============================= TODO: Also, check when it makes sense to have a staging buffer for copying data to the GPU
|
---|
| 71 | // and see if I actually need to use it everywhere I currently am. I think this is mentioned in Sascha WIllems dubo example
|
---|
| 72 | // or some other Vulkan website I recently bookmarked
|
---|
| 73 |
|
---|
| 74 | template<class T>
|
---|
| 75 | VulkanBuffer<T>::VulkanBuffer()
|
---|
| 76 | : alignment(0)
|
---|
[b7fc3c2] | 77 | , range(0)
|
---|
[a3cefaa] | 78 | , capacity(0)
|
---|
| 79 | , numObjects(0)
|
---|
[6bac215] | 80 | , resized(false)
|
---|
[a3cefaa] | 81 | , rawData(nullptr)
|
---|
[6486ba8] | 82 | , mappedData() {
|
---|
[a3cefaa] | 83 | }
|
---|
| 84 |
|
---|
| 85 | template<class T>
|
---|
[b7fc3c2] | 86 | VulkanBuffer<T>::VulkanBuffer(size_t capacity, size_t range, size_t minOffsetAlignment)
|
---|
| 87 | : alignment(range)
|
---|
| 88 | , range(range / sizeof(T))
|
---|
[a3cefaa] | 89 | , capacity(capacity)
|
---|
| 90 | , numObjects(0)
|
---|
[6bac215] | 91 | , resized(false)
|
---|
[a3cefaa] | 92 | , rawData(nullptr)
|
---|
[6486ba8] | 93 | , mappedData() {
|
---|
[a3cefaa] | 94 | if (minOffsetAlignment > 0) {
|
---|
| 95 | alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[6486ba8] | 98 | rawData = (T*)malloc(memRequirement(capacity));
|
---|
[a3cefaa] | 99 | }
|
---|
| 100 |
|
---|
[6bac215] | 101 | template<class T>
|
---|
| 102 | VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) {
|
---|
| 103 | // TODO: Implement
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[a3cefaa] | 106 | template<class T>
|
---|
| 107 | VulkanBuffer<T>::~VulkanBuffer() {
|
---|
[6486ba8] | 108 | if (rawData != nullptr) {
|
---|
| 109 | free(rawData);
|
---|
[a3cefaa] | 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | template<class T>
|
---|
[6bac215] | 114 | VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept {
|
---|
| 115 | if (this != &other) {
|
---|
| 116 | capacity = other.capacity;
|
---|
| 117 | numObjects = other.numObjects;
|
---|
| 118 | resized = other.resized;
|
---|
[a3cefaa] | 119 |
|
---|
[6bac215] | 120 | alignment = other.alignment;
|
---|
[b7fc3c2] | 121 | range = other.range;
|
---|
[a3cefaa] | 122 |
|
---|
[5ea0a37] | 123 | mappedData = other.mappedData;
|
---|
| 124 |
|
---|
[6486ba8] | 125 | if (rawData != nullptr) {
|
---|
| 126 | free(rawData);
|
---|
[6bac215] | 127 | }
|
---|
[a3cefaa] | 128 |
|
---|
[6486ba8] | 129 | rawData = other.rawData;
|
---|
[a3cefaa] | 130 |
|
---|
[6bac215] | 131 | other.capacity = 0;
|
---|
| 132 | other.numObjects = 0;
|
---|
[b7fc3c2] | 133 | other.range = 0;
|
---|
[a3cefaa] | 134 |
|
---|
[5ea0a37] | 135 | other.mappedData.clear();
|
---|
[6486ba8] | 136 | other.rawData = nullptr;
|
---|
[6bac215] | 137 | }
|
---|
[a3cefaa] | 138 |
|
---|
| 139 | return *this;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[6bac215] | 142 | template<class T>
|
---|
[5ea0a37] | 143 | size_t VulkanBuffer<T>::memorySize() {
|
---|
| 144 | return memRequirement(capacity);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | template<class T>
|
---|
| 148 | T* VulkanBuffer<T>::data() {
|
---|
| 149 | return rawData;
|
---|
[6bac215] | 150 | }
|
---|
| 151 |
|
---|
[6486ba8] | 152 | template<class T>
|
---|
| 153 | T& VulkanBuffer<T>::get(uint32_t index) {
|
---|
| 154 | // TODO: Check that index < numObjects
|
---|
| 155 |
|
---|
| 156 | T* obj = (T*)((size_t)rawData + memOffset(index));
|
---|
| 157 | return *obj;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[a3cefaa] | 160 | template<class T>
|
---|
[8dcbf62] | 161 | void VulkanBuffer<T>::add(T obj) {
|
---|
[5ea0a37] | 162 | // TODO: Maybe copy this to the resize() function and call that function here
|
---|
[6bac215] | 163 | if (numObjects == capacity) {
|
---|
| 164 | // Once I add Vulkan buffer objects in here, make sure this doesn't overlap with resizeBufferSet
|
---|
| 165 | resized = true;
|
---|
| 166 |
|
---|
[6486ba8] | 167 | size_t oldMemReq = memRequirement(capacity);
|
---|
| 168 |
|
---|
[6bac215] | 169 | capacity *= 2;
|
---|
[6486ba8] | 170 |
|
---|
| 171 | size_t newMemReq = memRequirement(capacity);
|
---|
| 172 |
|
---|
| 173 | T* newData = (T*)malloc(newMemReq);
|
---|
| 174 | // TODO: Check for failure
|
---|
| 175 |
|
---|
| 176 | memcpy(newData, rawData, oldMemReq);
|
---|
| 177 |
|
---|
| 178 | free(rawData);
|
---|
| 179 | rawData = newData;
|
---|
[6bac215] | 180 | }
|
---|
| 181 |
|
---|
[6486ba8] | 182 | T* ptr = (T*)((size_t)rawData + memOffset(numObjects));
|
---|
| 183 | *ptr = obj;
|
---|
| 184 |
|
---|
[8dcbf62] | 185 | numObjects++;
|
---|
[a3cefaa] | 186 | }
|
---|
| 187 |
|
---|
[90880fb] | 188 | template<class T>
|
---|
| 189 | void* VulkanBuffer<T>::mapped(size_t idx) {
|
---|
| 190 | return mappedData[idx];
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | template<class T>
|
---|
| 194 | void VulkanBuffer<T>::map(vector<VkDeviceMemory>& deviceMemory, VkDevice device) {
|
---|
| 195 | // TODO: Make sure that mappedData initally has size 0. If not, it means the memory is already mapped
|
---|
| 196 | // and I should return some kind of error or warning
|
---|
| 197 | mappedData.resize(deviceMemory.size());
|
---|
| 198 |
|
---|
| 199 | for (size_t i = 0; i < deviceMemory.size(); i++) {
|
---|
| 200 | vkMapMemory(device, deviceMemory[i], 0, memorySize(), 0, &mappedData[i]);
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | template<class T>
|
---|
| 205 | void VulkanBuffer<T>::unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device) {
|
---|
| 206 | for (size_t i = 0; i < deviceMemory.size(); i++) {
|
---|
| 207 | vkUnmapMemory(device, deviceMemory[i]);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | mappedData.clear();
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[b7fc3c2] | 213 | template<class T>
|
---|
[5ea0a37] | 214 | void VulkanBuffer<T>::resize() {
|
---|
| 215 | resized = false;
|
---|
[b7fc3c2] | 216 | }
|
---|
| 217 |
|
---|
| 218 | template<class T>
|
---|
| 219 | size_t VulkanBuffer<T>::memRequirement(size_t capacity) {
|
---|
| 220 | return (capacity / range) * alignment + (capacity % range) * sizeof(T);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[6486ba8] | 223 | template<class T>
|
---|
| 224 | size_t VulkanBuffer<T>::memOffset(uint32_t index) {
|
---|
| 225 | return (index / range) * alignment + (index % range) * sizeof(T);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[a3cefaa] | 228 | #endif // _VULKAN_BUFFER_H
|
---|