-
Notifications
You must be signed in to change notification settings - Fork 34
/
buffer.cc
434 lines (340 loc) · 13.6 KB
/
buffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <vkpp/buffer.hh>
#include <vkpp/queue.hh>
#include <vkpp/exception.hh>
#include <vkpp/debug_marker.hh>
#include <vkpp/command_buffer.hh>
#include <vkpp/device.hh>
#include <utility>
namespace vkpp {
Buffer::Buffer(Device& logical_device,
VkDeviceSize size_in_bytes,
VkBufferUsageFlags usage)
: size_in_bytes { size_in_bytes },
sharing_mode { VK_SHARING_MODE_EXCLUSIVE },
usage { usage },
device { logical_device.get_handle() } {
VkBufferCreateInfo create_info;
create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
create_info.pNext = nullptr;
create_info.flags = 0;
create_info.size = size_in_bytes;
create_info.usage = usage;
create_info.sharingMode = sharing_mode;
create_info.queueFamilyIndexCount = 0;
create_info.pQueueFamilyIndices = nullptr;
if (VkResult error = vkCreateBuffer(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create buffer!" };
}
}
Buffer::Buffer(Device& logical_device,
VkDeviceSize size_in_bytes,
VkBufferUsageFlags usage,
std::vector<Queue>& queue_families)
: size_in_bytes { size_in_bytes },
sharing_mode { VK_SHARING_MODE_CONCURRENT },
usage { usage },
device { logical_device.get_handle() } {
VkBufferCreateInfo create_info;
create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
create_info.pNext = nullptr;
create_info.flags = 0;
create_info.size = size_in_bytes;
create_info.sharingMode = sharing_mode;
create_info.usage = usage;
create_info.queueFamilyIndexCount = queue_families.size();
std::vector<std::uint32_t> indices(queue_families.size());
for (std::size_t i { 0 }; i < indices.size(); ++i) {
indices[i] = queue_families[i].get_family_index();
}
create_info.pQueueFamilyIndices = indices.data();
if (VkResult error = vkCreateBuffer(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create buffer!" };
}
}
Buffer::~Buffer() noexcept {
if (handle != VK_NULL_HANDLE) {
vkDestroyBuffer(device, handle, nullptr);
}
}
Buffer::Buffer(Buffer&& buffer) noexcept {
swap(*this, buffer);
}
Buffer& Buffer::operator=(Buffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
void swap(Buffer& lhs, Buffer& rhs) {
using std::swap;
swap(lhs.device, rhs.device);
swap(lhs.handle, rhs.handle);
swap(lhs.size_in_bytes, rhs.size_in_bytes);
swap(lhs.sharing_mode, rhs.sharing_mode);
swap(lhs.usage, rhs.usage);
}
VkBuffer& Buffer::get_handle() {
return handle;
}
VkMemoryRequirements Buffer::get_memory_requirements() const {
VkMemoryRequirements requirements;
vkGetBufferMemoryRequirements(device, handle, &requirements);
return requirements;
}
VkDeviceSize Buffer::get_size() const {
return size_in_bytes;
}
VkSharingMode Buffer::get_sharing_mode() const {
return sharing_mode;
}
VkBufferUsageFlags Buffer::get_usage() const {
return usage;
}
VkDeviceMemory& Buffer::get_bound_memory() {
return memory;
}
void Buffer::bind(DeviceMemory& device_memory, std::uint32_t offset) {
memory = device_memory.get_handle();
vkBindBufferMemory(device, handle, memory, offset);
}
DeviceBuffer::DeviceBuffer(Device& device,
CommandPool& command_pool,
const void* buffer,
VkDeviceSize size,
VkBufferUsageFlags usage)
: Buffer { device,
size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | usage } {
Buffer staging_buffer {
device,
size,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT
};
auto staging_memory_requirements = staging_buffer.get_memory_requirements();
DeviceMemory staging_memory {
device,
staging_memory_requirements,
DeviceMemory::Type::HostVisible
};
staging_buffer.bind(staging_memory);
staging_memory.copy(size, buffer);
auto buffer_memory_requirements = get_memory_requirements();
device_memory = DeviceMemory {
device,
buffer_memory_requirements,
DeviceMemory::Type::DeviceLocal
};
bind(device_memory);
auto command_buffer = command_pool.allocate_and_begin();
command_buffer.copy_buffer(staging_buffer, *this);
command_buffer.end();
command_pool.get_queue().submit(command_buffer)
.wait_idle();
}
DeviceBuffer::DeviceBuffer(Device& device,
VkDeviceSize size,
VkBufferUsageFlags usage)
: Buffer { device,
size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | usage } {
auto buffer_memory_requirements = get_memory_requirements();
device_memory = DeviceMemory {
device,
buffer_memory_requirements,
DeviceMemory::Type::DeviceLocal
};
bind(device_memory);
}
void swap(DeviceBuffer& lhs, DeviceBuffer& rhs) {
using std::swap;
swap(static_cast<Buffer&>(lhs), static_cast<Buffer&>(rhs));
swap(lhs.device_memory, rhs.device_memory);
}
DeviceBuffer& DeviceBuffer::operator=(DeviceBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
DeviceBuffer::DeviceBuffer(DeviceBuffer&& buffer) noexcept {
swap(*this, buffer);
}
DeviceMemory& DeviceBuffer::get_device_memory() {
return device_memory;
}
HostBuffer::HostBuffer(Device& device,
const void* buffer,
VkDeviceSize size,
VkBufferUsageFlags usage)
: Buffer { device,
size,
usage } {
auto memory_requirements = get_memory_requirements();
device_memory = DeviceMemory {
device,
memory_requirements,
DeviceMemory::Type::HostVisible
};
bind(device_memory);
device_memory.copy(size, buffer);
}
HostBuffer::HostBuffer(Device& device,
VkDeviceSize size,
VkBufferUsageFlags usage)
: Buffer { device,
size,
usage } {
auto memory_requirements = get_memory_requirements();
device_memory = DeviceMemory {
device,
memory_requirements,
DeviceMemory::Type::HostVisible
};
bind(device_memory);
}
void swap(HostBuffer& lhs, HostBuffer& rhs) {
using std::swap;
swap(static_cast<Buffer&>(lhs), static_cast<Buffer&>(rhs));
swap(lhs.device_memory, rhs.device_memory);
}
HostBuffer& HostBuffer::operator=(HostBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
HostBuffer::HostBuffer(HostBuffer&& buffer) noexcept {
swap(*this, buffer);
}
DeviceMemory& HostBuffer::get_device_memory() {
return device_memory;
}
void swap(VertexBuffer& lhs, VertexBuffer& rhs) {
using std::swap;
swap(static_cast<DeviceBuffer&>(lhs), static_cast<DeviceBuffer&>(rhs));
swap(lhs.element_count, rhs.element_count);
swap(lhs.binding, rhs.binding);
swap(lhs.attributes, rhs.attributes);
}
VertexBuffer& VertexBuffer::operator=(VertexBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
VertexBuffer::VertexBuffer(VertexBuffer&& buffer) noexcept {
swap(*this, buffer);
}
const VertexBuffer::Attributes& VertexBuffer::get_attributes() const {
return attributes;
}
std::uint32_t VertexBuffer::get_binding_id() const {
return binding.binding;
}
const VkVertexInputBindingDescription& VertexBuffer::get_binding() const {
return binding;
}
std::uint32_t VertexBuffer::count() const {
return static_cast<std::uint32_t>(element_count);
}
IndexBuffer::IndexBuffer(Device& device,
CommandPool& command_pool,
const std::vector<unsigned>& indices)
: DeviceBuffer { device,
command_pool,
indices.data(),
sizeof(indices[0]) * indices.size(),
VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT } {
this->element_count = indices.size();
this->index_type = VK_INDEX_TYPE_UINT32;
}
IndexBuffer::IndexBuffer(Device& device,
CommandPool& command_pool,
const std::vector<unsigned short>& indices)
: DeviceBuffer { device,
command_pool,
indices.data(),
sizeof(indices[0]) * indices.size(),
VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT } {
this->element_count = indices.size();
this->index_type = VK_INDEX_TYPE_UINT16;
}
void swap(IndexBuffer& lhs, IndexBuffer& rhs) {
using std::swap;
swap(static_cast<DeviceBuffer&>(lhs), static_cast<DeviceBuffer&>(rhs));
swap(lhs.element_count, rhs.element_count);
swap(lhs.index_type, rhs.index_type);
}
IndexBuffer& IndexBuffer::operator=(IndexBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
IndexBuffer::IndexBuffer(IndexBuffer&& buffer) noexcept {
swap(*this, buffer);
}
std::uint32_t IndexBuffer::count() const {
return static_cast<std::uint32_t>(element_count);
}
VkIndexType IndexBuffer::get_type() const {
return index_type;
}
UniformBuffer::UniformBuffer(Device& device,
VkDeviceSize size)
: HostBuffer { device, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT } { }
void swap(UniformBuffer& lhs, UniformBuffer& rhs) {
using std::swap;
swap(static_cast<HostBuffer&>(lhs), static_cast<HostBuffer&>(rhs));
}
UniformBuffer& UniformBuffer::operator=(UniformBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
UniformBuffer::UniformBuffer(UniformBuffer&& buffer) noexcept {
swap(*this, buffer);
}
std::vector<UniformBuffer> UniformBuffer::create(Device& device, VkDeviceSize size, std::size_t n, const char* name) {
std::vector<UniformBuffer> uniform_buffers;
uniform_buffers.reserve(n);
for (std::size_t i { 0 }; i < n; ++i) {
uniform_buffers.emplace_back(device, size);
DebugMarker::object_name(device, uniform_buffers.back(),
VK_OBJECT_TYPE_BUFFER, name);
std::string memory_name { name };
memory_name += " Device Memory";
DebugMarker::object_name(device, uniform_buffers.back().get_device_memory(),
VK_OBJECT_TYPE_DEVICE_MEMORY, memory_name.c_str());
} return uniform_buffers;
}
StorageBuffer::StorageBuffer(Device& device,
VkDeviceSize size_in_bytes)
: DeviceBuffer { device, size_in_bytes,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT } {
staging_buffer = Buffer {
device,
size_in_bytes,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT
};
auto staging_memory_requirements = staging_buffer.get_memory_requirements();
staging_memory = DeviceMemory {
device,
staging_memory_requirements,
DeviceMemory::Type::HostVisible
};
staging_buffer.bind(staging_memory);
}
void swap(StorageBuffer& lhs, StorageBuffer& rhs) {
using std::swap;
swap(static_cast<DeviceBuffer&>(lhs), static_cast<DeviceBuffer&>(rhs));
swap(lhs.staging_buffer, rhs.staging_buffer);
swap(lhs.staging_memory, rhs.staging_memory);
}
StorageBuffer& StorageBuffer::operator=(StorageBuffer&& buffer) noexcept {
swap(*this, buffer);
return *this;
}
StorageBuffer::StorageBuffer(StorageBuffer&& buffer) noexcept {
swap(*this, buffer);
}
Buffer& StorageBuffer::get_staging_buffer() {
return staging_buffer;
}
DeviceMemory& StorageBuffer::get_staging_memory() {
return staging_memory;
}
}