Changeset 6486ba8 in opengl-game for vulkan-buffer.hpp
- Timestamp:
- Jun 11, 2021, 8:12:29 PM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 5ea0a37
- Parents:
- c1ec4f6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-buffer.hpp
rc1ec4f6 r6486ba8 33 33 void resize(); 34 34 35 T& get(uint32_t index); 35 36 void add(T obj); 36 37 … … 41 42 size_t alignment; 42 43 size_t range; 44 //size_t capacity; 43 45 size_t numObjects; 44 46 45 T* srcData; // TODO: Rename this to something else probably and rename rawData to data 46 vector<T>* vData; 47 48 // Remember that this is a pointer to the mapped video memory 49 // Maybe rename it to mappedData or something to make that clearer 50 void* rawData; 47 T* rawData; 48 vector<void*> mappedData; 51 49 52 50 size_t memRequirement(size_t capacity); 51 size_t memOffset(uint32_t index); 53 52 }; 54 53 … … 73 72 , numObjects(0) 74 73 , resized(false) 75 , srcData(nullptr)76 74 , rawData(nullptr) 77 , vData(nullptr) {75 , mappedData() { 78 76 } 79 77 … … 85 83 , numObjects(0) 86 84 , resized(false) 87 , srcData(nullptr)88 85 , rawData(nullptr) 89 , vData(nullptr) {86 , mappedData() { 90 87 if (minOffsetAlignment > 0) { 91 88 alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1); 92 89 } 93 90 94 srcData = (T*)malloc(memRequirement(capacity));91 rawData = (T*)malloc(memRequirement(capacity)); 95 92 } 96 93 … … 102 99 template<class T> 103 100 VulkanBuffer<T>::~VulkanBuffer() { 104 if ( srcData != nullptr) {105 free( srcData);101 if (rawData != nullptr) { 102 free(rawData); 106 103 } 107 104 } … … 117 114 range = other.range; 118 115 119 if ( srcData != nullptr) {120 free( srcData);116 if (rawData != nullptr) { 117 free(rawData); 121 118 } 122 119 123 srcData = other.srcData;120 rawData = other.rawData; 124 121 125 122 other.capacity = 0; … … 127 124 other.range = 0; 128 125 129 other. srcData = nullptr;126 other.rawData = nullptr; 130 127 } 131 128 … … 139 136 140 137 template<class T> 138 T& VulkanBuffer<T>::get(uint32_t index) { 139 // TODO: Check that index < numObjects 140 141 T* obj = (T*)((size_t)rawData + memOffset(index)); 142 return *obj; 143 } 144 145 template<class T> 141 146 void VulkanBuffer<T>::add(T obj) { 142 147 if (numObjects == capacity) { … … 144 149 resized = true; 145 150 151 size_t oldMemReq = memRequirement(capacity); 152 146 153 capacity *= 2; 154 155 size_t newMemReq = memRequirement(capacity); 156 157 T* newData = (T*)malloc(newMemReq); 158 // TODO: Check for failure 159 160 memcpy(newData, rawData, oldMemReq); 161 162 free(rawData); 163 rawData = newData; 147 164 } 165 166 T* ptr = (T*)((size_t)rawData + memOffset(numObjects)); 167 *ptr = obj; 148 168 149 169 numObjects++; … … 160 180 } 161 181 182 template<class T> 183 size_t VulkanBuffer<T>::memOffset(uint32_t index) { 184 return (index / range) * alignment + (index % range) * sizeof(T); 185 } 186 162 187 #endif // _VULKAN_BUFFER_H
Note:
See TracChangeset
for help on using the changeset viewer.