source: opengl-game/vulkan-buffer.hpp@ 6486ba8

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

Rewrite some parts of SDLGame and VulkanGame to store per-object buffer object
data contiguously and copied to the GPU in one call

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