CMSC23740 Common Code Library
Support code for CS23740 programming projects
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1
8/*
9 * COPYRIGHT (c) 2024 John Reppy (https://cs.uchicago.edu/~jhr)
10 * All rights reserved.
11 */
12
13#ifndef _CS237_BUFFER_HPP_
14#define _CS237_BUFFER_HPP_
15
16#ifndef _CS237_HPP_
17#error "cs237/buffer.hpp should not be included directly"
18#endif
19
20namespace cs237 {
21
23class Buffer {
24public:
26 vk::Buffer vkBuffer () const { return this->_buf; }
27
29 MemoryObj *memory () const { return this->_mem; }
30
32 vk::MemoryRequirements requirements ()
33 {
34 vk::MemoryRequirements reqs;
35 this->_app->_device.getBufferMemoryRequirements(this->_buf, &reqs);
36 return reqs;
37 }
38
39protected:
41 vk::Buffer _buf;
43
48 Buffer (Application *app, vk::BufferUsageFlags usage, size_t sz)
49 : _app(app)
50 {
51 vk::BufferCreateInfo info(
52 {}, /* flags */
53 sz,
54 usage,
55 vk::SharingMode::eExclusive, /* sharingMode */
56 {}); /* queueFamilyIndices */
57
58 this->_buf = app->_device.createBuffer (info);
59 this->_mem = new MemoryObj(app, this->requirements());
60
61 // bind the memory object to the buffer
62 this->_app->_device.bindBufferMemory(this->_buf, this->_mem->_mem, 0);
63
64 }
65
68 {
69 delete this->_mem;
70 this->_app->_device.destroyBuffer (this->_buf, nullptr);
71 }
72
78 void _copyTo (const void *src, size_t offset, size_t sz)
79 {
80 this->_mem->copyTo(src, offset, sz);
81 }
82
85 void _copyTo (const void *src) { this->_mem->copyTo(src); }
86
87};
88
91template <typename V>
92class VertexBuffer : public Buffer {
93public:
94
96 using VertexType = V;
97
104 VertexBuffer (Application *app, uint32_t nVerts)
105 : Buffer (app, vk::BufferUsageFlagBits::eVertexBuffer, nVerts*sizeof(V))
106 { }
107
114 VertexBuffer (Application *app, vk::ArrayProxy<V> const &src)
115 : VertexBuffer(app, src.size())
116 {
117 this->copyTo(src);
118 }
119
122 void copyTo (vk::ArrayProxy<V> const &src)
123 {
124 assert ((src.size() * sizeof(V) <= this->_mem->size()) && "src is too large");
125 this->_copyTo(src.data(), 0, src.size()*sizeof(V));
126 }
127
131 void copyTo (vk::ArrayProxy<V> const &src, uint32_t offset)
132 {
133 assert (((src.size()+offset) * sizeof(V) <= this->_mem->size())
134 && "src is too large");
135 this->_copyTo(src.data(), offset*sizeof(V), src.size()*sizeof(V));
136 }
137
138};
139
141template <typename I>
142class IndexBuffer : public Buffer {
143public:
144
146 using IndexType = I;
147
152 : Buffer (app, vk::BufferUsageFlagBits::eIndexBuffer, nIndices*sizeof(I)),
153 _nIndices(nIndices)
154 { }
155
159 IndexBuffer (Application *app, vk::ArrayProxy<I> const &src)
160 : IndexBuffer(app, src.size())
161 {
162 this->copyTo(src);
163 }
164
166 uint32_t nIndices () const { return this->_nIndices; }
167
170 void copyTo (vk::ArrayProxy<I> const &src)
171 {
172 assert ((src.size() * sizeof(I) <= this->_mem->size()) && "src is too large");
173 this->_copyTo(src.data(), 0, src.size()*sizeof(I));
174 }
175
179 void copyTo (vk::ArrayProxy<I> const &src, uint32_t offset)
180 {
181 assert (((src.size()+offset) * sizeof(I) <= this->_mem->size())
182 && "src is too large");
183 this->_copyTo(src.data(), offset*sizeof(I), src.size()*sizeof(I));
184 }
185
186private:
187 uint32_t _nIndices;
188
189};
190
193template <typename UB>
194class UniformBuffer : public Buffer {
195public:
196
198 using BufferType = UB;
199
203 : Buffer (app, vk::BufferUsageFlagBits::eUniformBuffer, sizeof(UB))
204 { }
205
209 UniformBuffer (Application *app, UB const &src)
210 : UniformBuffer(app)
211 {
212 this->copyTo(src);
213 }
214
217 void copyTo (UB const &src)
218 {
219 this->_copyTo(&src, 0, sizeof(UB));
220 }
221
223 vk::DescriptorBufferInfo descInfo ()
224 {
225 return vk::DescriptorBufferInfo(this->_buf, 0, sizeof(UB));
226 }
227
228};
229
233template <typename SV>
235public:
236
238 using BufferType = SV;
239
243 StorageVertexBuffer (Application *app, uint32_t nVerts)
244 : Buffer (app, vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eVertexBuffer, nVerts*sizeof(SV))
245 { }
246
250 StorageVertexBuffer (Application *app, vk::ArrayProxy<SV> const &src)
251 : StorageVertexBuffer(app, src.size())
252 {
253 this->copyTo(src);
254 }
255
258 void copyTo (vk::ArrayProxy<SV> const &src)
259 {
260 assert ((src.size() * sizeof(SV) <= this->_mem->size()) && "src is too large");
261 this->_copyTo(src.data(), 0, src.size()*sizeof(SV));
262 }
263
267 void copyTo (vk::ArrayProxy<SV> const &src, uint32_t offset)
268 {
269 assert (((src.size()+offset) * sizeof(SV) <= this->_mem->size())
270 && "src is too large");
271 this->_copyTo(src.data(), offset*sizeof(SV), src.size()*sizeof(SV));
272 }
273
275 vk::DescriptorBufferInfo descInfo ()
276 {
277 return vk::DescriptorBufferInfo(this->_buf, 0, sizeof(SV));
278 }
279
280};
281
284template <typename S>
285class StorageBuffer : public Buffer {
286public:
287
289 using BufferType = S;
290
294 : Buffer (app, vk::BufferUsageFlagBits::eStorageBuffer, sizeof(S))
295 { }
296
300 StorageBuffer (Application *app, S const &src)
301 : StorageBuffer(app)
302 {
303 this->copyTo(src);
304 }
305
309 StorageBuffer (Application *app, vk::ArrayProxy<S> const &src)
310 : Buffer (app, vk::BufferUsageFlagBits::eStorageBuffer, src.size()*sizeof(S))
311 {
312 this->copyTo(src.data());
313 }
314
317
318 void copyTo (S const &src)
319 {
320 this->_copyTo(&src, 0, sizeof(S));
321 }
322
324 vk::DescriptorBufferInfo descInfo ()
325 {
326 return vk::DescriptorBufferInfo(this->_buf, 0, sizeof(S));
327 }
328
331 void copyTo (vk::ArrayProxy<S> const &src)
332 {
333 assert ((src.size() * sizeof(S) <= this->_mem->size()) && "src is too large");
334 this->_copyTo(src.data(), 0, src.size()*sizeof(S));
335 }
336
340 void copyTo (vk::ArrayProxy<S> const &src, uint32_t offset)
341 {
342 assert (((src.size()+offset) * sizeof(S) <= this->_mem->size())
343 && "src is too large");
344 this->_copyTo(src.data(), offset*sizeof(S), src.size()*sizeof(S));
345 }
346
347};
348
349} // namespace cs237
350
351#endif // !_CS237_BUFFER_HPP_
the base class for applications
Definition application.hpp:25
vk::Device _device
the logical device that we are using to render
Definition application.hpp:443
A base class for buffer objects of all kinds.
Definition buffer.hpp:23
void _copyTo(const void *src)
Definition buffer.hpp:85
vk::MemoryRequirements requirements()
get the memory requirements of this buffer
Definition buffer.hpp:32
~Buffer()
destructor
Definition buffer.hpp:67
vk::Buffer _buf
the Vulkan buffer object
Definition buffer.hpp:41
MemoryObj * _mem
the Vulkan memory object that holds the buffer
Definition buffer.hpp:42
Buffer(Application *app, vk::BufferUsageFlags usage, size_t sz)
Definition buffer.hpp:48
void _copyTo(const void *src, size_t offset, size_t sz)
Definition buffer.hpp:78
MemoryObj * memory() const
get the memory object for this buffer
Definition buffer.hpp:29
Application * _app
the application
Definition buffer.hpp:40
vk::Buffer vkBuffer() const
get the Vulkan buffer object for this buffer
Definition buffer.hpp:26
Buffer class for index data; the type parameter I is the index type.
Definition buffer.hpp:142
I IndexType
the type of indices
Definition buffer.hpp:146
void copyTo(vk::ArrayProxy< I > const &src)
Definition buffer.hpp:170
uint32_t nIndices() const
get the number of indices in the buffer
Definition buffer.hpp:166
void copyTo(vk::ArrayProxy< I > const &src, uint32_t offset)
Definition buffer.hpp:179
IndexBuffer(Application *app, vk::ArrayProxy< I > const &src)
Definition buffer.hpp:159
IndexBuffer(Application *app, uint32_t nIndices)
Definition buffer.hpp:151
wrapper around Vulkan memory objects
Definition memory-obj.hpp:23
vk::DeviceMemory _mem
the device memory object
Definition memory-obj.hpp:59
void copyTo(const void *src, size_t offset, size_t sz)
Definition memory-obj.hpp:34
Definition buffer.hpp:285
StorageBuffer(Application *app, S const &src)
Definition buffer.hpp:300
void copyTo(S const &src)
Definition buffer.hpp:318
void copyTo(vk::ArrayProxy< S > const &src, uint32_t offset)
Definition buffer.hpp:340
S BufferType
the type of the buffer's contents
Definition buffer.hpp:289
vk::DescriptorBufferInfo descInfo()
get the default buffer-descriptor info for this buffer
Definition buffer.hpp:324
void copyTo(vk::ArrayProxy< S > const &src)
Definition buffer.hpp:331
StorageBuffer(Application *app)
Definition buffer.hpp:293
StorageBuffer(Application *app, vk::ArrayProxy< S > const &src)
Definition buffer.hpp:309
Definition buffer.hpp:234
StorageVertexBuffer(Application *app, vk::ArrayProxy< SV > const &src)
Definition buffer.hpp:250
void copyTo(vk::ArrayProxy< SV > const &src, uint32_t offset)
Definition buffer.hpp:267
void copyTo(vk::ArrayProxy< SV > const &src)
Definition buffer.hpp:258
SV BufferType
the type of the buffer's contents
Definition buffer.hpp:238
StorageVertexBuffer(Application *app, uint32_t nVerts)
Definition buffer.hpp:243
vk::DescriptorBufferInfo descInfo()
get the default buffer-descriptor info for this buffer
Definition buffer.hpp:275
Definition buffer.hpp:194
void copyTo(UB const &src)
Definition buffer.hpp:217
vk::DescriptorBufferInfo descInfo()
get the default buffer-descriptor info for this buffer
Definition buffer.hpp:223
UniformBuffer(Application *app)
Definition buffer.hpp:202
UniformBuffer(Application *app, UB const &src)
Definition buffer.hpp:209
UB BufferType
the type of the buffer's contents
Definition buffer.hpp:198
Definition buffer.hpp:92
V VertexType
the type of vertices
Definition buffer.hpp:96
void copyTo(vk::ArrayProxy< V > const &src)
Definition buffer.hpp:122
VertexBuffer(Application *app, uint32_t nVerts)
Definition buffer.hpp:104
VertexBuffer(Application *app, vk::ArrayProxy< V > const &src)
Definition buffer.hpp:114
void copyTo(vk::ArrayProxy< V > const &src, uint32_t offset)
Definition buffer.hpp:131
Definition aabb.hpp:22