source: opengl-game/vulkan-buffer.hpp@ 5ea0a37

feature/imgui-sdl
Last change on this file since 5ea0a37 was 5ea0a37, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Add a function to VulkanBuffer to return a pointer to the buffer memory, and replace the use of updateBufferSet with copyDataToMemory to update all the data for a single pipeline in one call

  • Property mode set to 100644
File size: 5.4 KB
Line 
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
8template<class T>
9class VulkanBuffer {
10
11 public:
12
13 // TODO: Make these private (maybe make a getter for numObjects)
14 // Externally, they are only used in resizeBufferSet
15 size_t capacity;
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;
21
22 VulkanBuffer();
23 VulkanBuffer(size_t capacity, size_t range, size_t minOffsetAlignment);
24
25 VulkanBuffer(const VulkanBuffer<T>&) = delete;
26 VulkanBuffer(VulkanBuffer<T>&& other);
27
28 ~VulkanBuffer();
29
30 VulkanBuffer<T>& operator=(const VulkanBuffer<T>&) = delete;
31 VulkanBuffer<T>& operator=(VulkanBuffer<T>&& other) noexcept;
32
33 size_t memorySize();
34
35 T* data(); // Not sure I need to expose this
36
37 T& get(uint32_t index);
38 void add(T obj);
39
40 void resize();
41
42 private:
43
44 size_t alignment;
45 size_t range;
46 //size_t capacity;
47 size_t numObjects;
48
49 T* rawData;
50 vector<void*> mappedData;
51
52 size_t memRequirement(size_t capacity);
53 size_t memOffset(uint32_t index);
54};
55
56// Currently, for SSBOs, I store the per-object values (usually just the model matrix), on each object, so they
57// are not in their own array and therefore cannot be pushed to the GPU as one block. The updates must happen
58// separately per object.
59
60// Since Sascha WIllems' dynamic UBO example works the same way (iirc), I can implement dynamic UBOs like that as well
61// for now. Would be nice to plan for potentially storing the ubo data on the CPU in a contiguous block in the future,
62// assuming that would make updates easier. Keep in mind that this only makes sense if all or most of the objects
63// in the ubo get updated every frame.
64
65// ============================= TODO: Also, check when it makes sense to have a staging buffer for copying data to the GPU
66// and see if I actually need to use it everywhere I currently am. I think this is mentioned in Sascha WIllems dubo example
67// or some other Vulkan website I recently bookmarked
68
69template<class T>
70VulkanBuffer<T>::VulkanBuffer()
71 : alignment(0)
72 , range(0)
73 , capacity(0)
74 , numObjects(0)
75 , resized(false)
76 , rawData(nullptr)
77 , mappedData() {
78}
79
80template<class T>
81VulkanBuffer<T>::VulkanBuffer(size_t capacity, size_t range, size_t minOffsetAlignment)
82 : alignment(range)
83 , range(range / sizeof(T))
84 , capacity(capacity)
85 , numObjects(0)
86 , resized(false)
87 , rawData(nullptr)
88 , mappedData() {
89 if (minOffsetAlignment > 0) {
90 alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
91 }
92
93 rawData = (T*)malloc(memRequirement(capacity));
94}
95
96template<class T>
97VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) {
98 // TODO: Implement
99}
100
101template<class T>
102VulkanBuffer<T>::~VulkanBuffer() {
103 if (rawData != nullptr) {
104 free(rawData);
105 }
106}
107
108template<class T>
109VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept {
110 if (this != &other) {
111 capacity = other.capacity;
112 numObjects = other.numObjects;
113 resized = other.resized;
114
115 alignment = other.alignment;
116 range = other.range;
117
118 mappedData = other.mappedData;
119
120 if (rawData != nullptr) {
121 free(rawData);
122 }
123
124 rawData = other.rawData;
125
126 other.capacity = 0;
127 other.numObjects = 0;
128 other.range = 0;
129
130 other.mappedData.clear();
131 other.rawData = nullptr;
132 }
133
134 return *this;
135}
136
137template<class T>
138size_t VulkanBuffer<T>::memorySize() {
139 return memRequirement(capacity);
140}
141
142template<class T>
143T* VulkanBuffer<T>::data() {
144 return rawData;
145}
146
147template<class T>
148T& VulkanBuffer<T>::get(uint32_t index) {
149 // TODO: Check that index < numObjects
150
151 T* obj = (T*)((size_t)rawData + memOffset(index));
152 return *obj;
153}
154
155template<class T>
156void VulkanBuffer<T>::add(T obj) {
157 // TODO: Maybe copy this to the resize() function and call that function here
158 if (numObjects == capacity) {
159 // Once I add Vulkan buffer objects in here, make sure this doesn't overlap with resizeBufferSet
160 resized = true;
161
162 size_t oldMemReq = memRequirement(capacity);
163
164 capacity *= 2;
165
166 size_t newMemReq = memRequirement(capacity);
167
168 T* newData = (T*)malloc(newMemReq);
169 // TODO: Check for failure
170
171 memcpy(newData, rawData, oldMemReq);
172
173 free(rawData);
174 rawData = newData;
175 }
176
177 T* ptr = (T*)((size_t)rawData + memOffset(numObjects));
178 *ptr = obj;
179
180 numObjects++;
181}
182
183template<class T>
184void VulkanBuffer<T>::resize() {
185 resized = false;
186}
187
188template<class T>
189size_t VulkanBuffer<T>::memRequirement(size_t capacity) {
190 return (capacity / range) * alignment + (capacity % range) * sizeof(T);
191}
192
193template<class T>
194size_t VulkanBuffer<T>::memOffset(uint32_t index) {
195 return (index / range) * alignment + (index % range) * sizeof(T);
196}
197
198#endif // _VULKAN_BUFFER_H
Note: See TracBrowser for help on using the repository browser.