CMSC23700 Common Code Library
Support code for CS23700 programming projects
Loading...
Searching...
No Matches
cs237-buffer.hpp
Go to the documentation of this file.
1
8/*
9 * COPYRIGHT (c) 2023 John Reppy (http://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 const 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
101 VertexBuffer (Application *app, uint32_t nVerts)
102 : Buffer (app, vk::BufferUsageFlagBits::eVertexBuffer, nVerts*sizeof(V))
103 { }
104
108 VertexBuffer (Application *app, vk::ArrayProxy<V> const &src)
109 : VertexBuffer(app, src.size())
110 {
111 this->copyTo(src);
112 }
113
116 void copyTo (vk::ArrayProxy<V> const &src)
117 {
118 assert ((src.size() * sizeof(V) <= this->_mem->size()) && "src is too large");
119 this->_copyTo(src.data(), 0, src.size()*sizeof(V));
120 }
121
125 void copyTo (vk::ArrayProxy<V> const &src, uint32_t offset)
126 {
127 assert (((src.size()+offset) * sizeof(V) <= this->_mem->size())
128 && "src is too large");
129 this->_copyTo(src.data(), offset*sizeof(V), src.size()*sizeof(V));
130 }
131
132};
133
135template <typename I>
136class IndexBuffer : public Buffer {
137public:
138
140 using IndexType = I;
141
146 : Buffer (app, vk::BufferUsageFlagBits::eIndexBuffer, nIndices*sizeof(I)),
147 _nIndices(nIndices)
148 { }
149
153 IndexBuffer (Application *app, vk::ArrayProxy<I> const &src)
154 : IndexBuffer(app, src.size())
155 {
156 this->copyTo(src);
157 }
158
160 uint32_t nIndices () const { return this->_nIndices; }
161
164 void copyTo (vk::ArrayProxy<I> const &src)
165 {
166 assert ((src.size() * sizeof(I) <= this->_mem->size()) && "src is too large");
167 this->_copyTo(src.data(), 0, src.size()*sizeof(I));
168 }
169
173 void copyTo (vk::ArrayProxy<I> const &src, uint32_t offset)
174 {
175 assert (((src.size()+offset) * sizeof(I) <= this->_mem->size())
176 && "src is too large");
177 this->_copyTo(src.data(), offset*sizeof(I), src.size()*sizeof(I));
178 }
179
180private:
181 uint32_t _nIndices;
182
183};
184
187template <typename UB>
188class UniformBuffer : public Buffer {
189public:
190
192 using BufferType = UB;
193
197 : Buffer (app, vk::BufferUsageFlagBits::eUniformBuffer, sizeof(UB))
198 { }
199
203 UniformBuffer (Application *app, UB const &src)
204 : UniformBuffer(app)
205 {
206 this->copyTo(src);
207 }
208
211 void copyTo (UB const &src)
212 {
213 this->_copyTo(&src, 0, sizeof(UB));
214 }
215
217 vk::DescriptorBufferInfo descInfo ()
218 {
219 return vk::DescriptorBufferInfo(this->_buf, 0, sizeof(UB));
220 }
221
222};
223
224} // namespace cs237
225
226#endif // !_CS237_BUFFER_HPP_
the base class for applications
Definition cs237-application.hpp:25
vk::Device _device
the logical device that we are using to render
Definition cs237-application.hpp:380
A base class for buffer objects of all kinds.
Definition cs237-buffer.hpp:23
void _copyTo(const void *src)
Definition cs237-buffer.hpp:85
vk::MemoryRequirements requirements()
get the memory requirements of this buffer
Definition cs237-buffer.hpp:32
~Buffer()
destructor
Definition cs237-buffer.hpp:67
vk::Buffer _buf
the Vulkan buffer object
Definition cs237-buffer.hpp:41
MemoryObj * _mem
the Vulkan memory object that holds the buffer
Definition cs237-buffer.hpp:42
Buffer(Application *app, vk::BufferUsageFlags usage, size_t sz)
Definition cs237-buffer.hpp:48
void _copyTo(const void *src, size_t offset, size_t sz)
Definition cs237-buffer.hpp:78
const MemoryObj * memory() const
get the memory object for this buffer
Definition cs237-buffer.hpp:29
Application * _app
the application
Definition cs237-buffer.hpp:40
vk::Buffer vkBuffer() const
get the Vulkan buffer object for this buffer
Definition cs237-buffer.hpp:26
Buffer class for index data; the type parameter I is the index type.
Definition cs237-buffer.hpp:136
I IndexType
the type of indices
Definition cs237-buffer.hpp:140
void copyTo(vk::ArrayProxy< I > const &src)
Definition cs237-buffer.hpp:164
uint32_t nIndices() const
get the number of indices in the buffer
Definition cs237-buffer.hpp:160
void copyTo(vk::ArrayProxy< I > const &src, uint32_t offset)
Definition cs237-buffer.hpp:173
IndexBuffer(Application *app, vk::ArrayProxy< I > const &src)
Definition cs237-buffer.hpp:153
IndexBuffer(Application *app, uint32_t nIndices)
Definition cs237-buffer.hpp:145
wrapper around Vulkan memory objects
Definition cs237-memory-obj.hpp:23
vk::DeviceMemory _mem
the device memory object
Definition cs237-memory-obj.hpp:57
void copyTo(const void *src, size_t offset, size_t sz)
Definition cs237-memory-obj.hpp:34
Definition cs237-buffer.hpp:188
void copyTo(UB const &src)
Definition cs237-buffer.hpp:211
vk::DescriptorBufferInfo descInfo()
get the default buffer-descriptor info for this buffer
Definition cs237-buffer.hpp:217
UniformBuffer(Application *app)
Definition cs237-buffer.hpp:196
UniformBuffer(Application *app, UB const &src)
Definition cs237-buffer.hpp:203
UB BufferType
the type of the buffer's contents
Definition cs237-buffer.hpp:192
Definition cs237-buffer.hpp:92
V VertexType
the type of vertices
Definition cs237-buffer.hpp:96
void copyTo(vk::ArrayProxy< V > const &src)
Definition cs237-buffer.hpp:116
VertexBuffer(Application *app, uint32_t nVerts)
Definition cs237-buffer.hpp:101
VertexBuffer(Application *app, vk::ArrayProxy< V > const &src)
Definition cs237-buffer.hpp:108
void copyTo(vk::ArrayProxy< V > const &src, uint32_t offset)
Definition cs237-buffer.hpp:125
Definition cs237-aabb.hpp:22