-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
106 lines (79 loc) · 2.81 KB
/
buffer.cpp
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
#include "stdafx.h"
#include "buffer.h"
#include "renderer.h"
VmaAlloc<vk::Buffer> buffer::createCombinedBuffer(const std::vector<buffer*>& buffers, VmaMemoryUsage memUsage, vk::BufferUsageFlags bufferUsage)
{
vk::DeviceSize size = 0;
for (auto buffer : buffers)
{
if (buffer->alignment() != 0)
size = align_offset(size, buffer->alignment());
buffer->m_offset = size;
size = buffer->m_offset + buffer->m_size;
bufferUsage |= buffer->buffer_usage();
}
auto alloc = Renderer::createBuffer(size, bufferUsage, memUsage);
for (auto buffer : buffers)
{
buffer->m_handle = alloc.value;
}
return alloc;
}
void buffer::updateBuffers(const VmaAlloc<vk::Buffer>& alloc, const std::vector<buffer*>& buffers, const std::vector<void*> bufferData)
{
VmaAllocationInfo info;
vmaGetAllocationInfo(vkRenderCtx.allocator, alloc.allocation, &info);
auto memoryProperties = vkRenderCtx.physicalDevice.getMemoryProperties();
if (memoryProperties.memoryTypes[info.memoryType].propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible)
{
intptr_t data;
vmaMapMemory(vkRenderCtx.allocator, alloc.allocation, reinterpret_cast<void**>(&data));
for (size_t i = 0; i < buffers.size(); ++i)
{
memcpy(reinterpret_cast<void*>(data + buffers[i]->m_offset), bufferData[i], buffers[i]->m_size);
}
vmaUnmapMemory(vkRenderCtx.allocator, alloc.allocation);
}
else
{
vk::DeviceSize totalSize = 0;
std::vector<vk::BufferCopy> copyData;
for (size_t i = 0; i < buffers.size(); ++i)
{
copyData.emplace_back(totalSize, buffers[i]->m_offset, buffers[i]->m_size);
totalSize += buffers[i]->m_size;
}
auto stagingBuffer = Renderer::createBufferUnique(totalSize, vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY);
intptr_t data;
vmaMapMemory(vkRenderCtx.allocator, stagingBuffer->allocation, reinterpret_cast<void**>(&data));
for (size_t i = 0; i < buffers.size(); ++i)
{
memcpy(reinterpret_cast<void*>(data+copyData[i].srcOffset), bufferData[i], buffers[i]->m_size);
}
vmaUnmapMemory(vkRenderCtx.allocator, stagingBuffer->allocation);
Renderer::copyBuffer(*stagingBuffer, alloc, copyData);
}
}
template<typename IndexType>
vk::DeviceSize index_buffer<IndexType>::alignment()
{
return sizeof(IndexType);
}
template<typename IndexType>
vk::BufferUsageFlags index_buffer<IndexType>::buffer_usage()
{
return vk::BufferUsageFlagBits::eIndexBuffer;
}
template<typename T>
vk::IndexType index_type();
template<>
vk::IndexType index_type<uint32_t>() { return vk::IndexType::eUint32; };
template<>
vk::IndexType index_type<uint16_t>() { return vk::IndexType::eUint16; };
template<typename IndexType>
void index_buffer<IndexType>::bind(vk::CommandBuffer cmd)
{
cmd.bindIndexBuffer(m_handle, m_offset, index_type<IndexType>() );
}
template class index_buffer<uint16_t>;
template class index_buffer<uint32_t>;