-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVulkanBuffer.cpp
More file actions
274 lines (241 loc) · 8.89 KB
/
Copy pathVulkanBuffer.cpp
File metadata and controls
274 lines (241 loc) · 8.89 KB
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
#include "VulkanBuffer.h"
#include "VulkanContext.h"
#include "VulkanCommandBufferPool.h"
#include "VulkanCommandBuffer.h"
#include "VulkanQueueManager.h"
#include "Core/Logger.h"
#include <cassert>
#include <cstring>
#include <stdexcept>
namespace sh::render::vk
{
VulkanBuffer::VulkanBuffer(const VulkanContext& context) :
context(context),
buffer(nullptr), bufferMem(nullptr), dataPtr(nullptr),
bufferInfo(), memProperty(0),
bPersistentMapping(false),
size(0)
{
}
VulkanBuffer::VulkanBuffer(const VulkanBuffer& other) :
context(other.context),
buffer(nullptr), bufferMem(nullptr), dataPtr(nullptr),
bufferInfo(other.bufferInfo), memProperty(other.memProperty),
bPersistentMapping(other.bPersistentMapping),
size(other.size)
{
operator=(other);
}
VulkanBuffer::VulkanBuffer(VulkanBuffer&& other) noexcept :
context(other.context),
buffer(other.buffer), bufferMem(other.bufferMem), dataPtr(other.dataPtr),
bufferInfo(other.bufferInfo), memProperty(other.memProperty),
bPersistentMapping(other.bPersistentMapping),
size(other.size)
{
other.buffer = nullptr;
other.bufferMem = nullptr;
other.dataPtr = nullptr;
}
VulkanBuffer::~VulkanBuffer()
{
Clean();
}
SH_RENDER_API auto VulkanBuffer::operator=(const VulkanBuffer& other) -> VulkanBuffer&
{
if (&other == this)
return *this;
Create(other.size, other.bufferInfo.usage, other.bufferInfo.sharingMode, other.memProperty, other.bPersistentMapping);
std::vector<uint8_t> temp = other.GetData();
SetData(temp.data());
return *this;
}
SH_RENDER_API auto VulkanBuffer::operator=(VulkanBuffer&& other) noexcept -> VulkanBuffer&
{
if (&other == this)
return *this;
Clean();
buffer = other.buffer;
bufferMem = other.bufferMem;
dataPtr = other.dataPtr;
bufferInfo = other.bufferInfo;
memProperty = other.memProperty;
bPersistentMapping = other.bPersistentMapping;
size = other.size;
other.buffer = nullptr;
other.bufferMem = nullptr;
other.dataPtr = nullptr;
return *this;
}
SH_RENDER_API auto VulkanBuffer::Resize(std::size_t size) -> bool
{
Clean();
const VkResult result = Create(size, bufferInfo.usage, bufferInfo.sharingMode, memProperty, this->bPersistentMapping);
if (result == VkResult::VK_SUCCESS)
return true;
return false;
}
SH_RENDER_API void VulkanBuffer::Clean()
{
if (buffer)
{
vmaDestroyBuffer(context.GetAllocator(), buffer, bufferMem);
//vkDestroyBuffer(device, buffer, nullptr);
buffer = nullptr;
bufferMem = nullptr;
dataPtr = nullptr;
}
/*if (bufferMem)
{
vkFreeMemory(device, bufferMem, nullptr);
bufferMem = nullptr;
}*/
}
SH_RENDER_API auto VulkanBuffer::Create(size_t size, VkBufferUsageFlags usageBits, VkSharingMode sharing, VkMemoryPropertyFlags memPropFlagBits, bool bPersistentMapping) -> VkResult
{
this->size = size;
this->bPersistentMapping = bPersistentMapping;
this->memProperty = memPropFlagBits;
Clean();
bufferInfo.sType = VkStructureType::VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usageBits;
bufferInfo.sharingMode = sharing;
/*VkResult result = vkCreateBuffer(device, &bufferInfo, nullptr, &buffer);
assert(result == VK_SUCCESS);
if (result != VK_SUCCESS)
return result;
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
uint32_t idx = FindMemoryType(memRequirements.memoryTypeBits, memPropFlagBits);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VkStructureType::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = idx;
result = vkAllocateMemory(device, &allocInfo, nullptr, &bufferMem);*/
const bool bUseMap = (memPropFlagBits & VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
const bool bGPUOnly = (memPropFlagBits & VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
if (bGPUOnly)
this->bPersistentMapping = false;
VmaAllocationCreateInfo allocCreateInfo{};
allocCreateInfo.usage = VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO;
allocCreateInfo.flags = 0;
if (bUseMap)
allocCreateInfo.flags |= VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
if (this->bPersistentMapping)
allocCreateInfo.flags |= VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT;
VmaAllocationInfo allocInfo{};
VkResult result = vmaCreateBuffer(context.GetAllocator(), &bufferInfo, &allocCreateInfo, &buffer, &bufferMem, &allocInfo);
if (result != VkResult::VK_SUCCESS)
{
assert(result == VkResult::VK_SUCCESS);
SH_ERROR_FORMAT("Failed to create buffer!: {}", string_VkResult(result));
return result;
}
if (this->bPersistentMapping)
dataPtr = allocInfo.pMappedData;
//result = vkBindBufferMemory(device, buffer, bufferMem, 0);
//assert(result == VkResult::VK_SUCCESS);
return result;
}
SH_RENDER_API void VulkanBuffer::SetData(const void* data)
{
SetData(data, 0, static_cast<std::size_t>(bufferInfo.size));
}
SH_RENDER_API void VulkanBuffer::SetData(const void* data, std::size_t offset, std::size_t size)
{
assert(buffer);
if (buffer == nullptr)
return;
const bool bGPUOnly = (memProperty & VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
if (!bGPUOnly)
{
if (!bPersistentMapping)
{
vmaMapMemory(context.GetAllocator(), bufferMem, &this->dataPtr);
std::memcpy(reinterpret_cast<uint8_t*>(dataPtr) + offset, data, size);
vmaUnmapMemory(context.GetAllocator(), bufferMem);
dataPtr = nullptr;
}
else
std::memcpy(reinterpret_cast<uint8_t*>(dataPtr) + offset, data, size);
}
else
{
VulkanBuffer stagingBuffer{ context };
stagingBuffer.Create(size, VkBufferUsageFlagBits::VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VkSharingMode::VK_SHARING_MODE_EXCLUSIVE,
VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
stagingBuffer.SetData(data);
VulkanCommandBuffer& cmd = *context.GetCommandBufferPool().AllocateCommandBuffer(std::this_thread::get_id(), VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT);
cmd.Build(
[&]()
{
VkBufferCopy cpy{};
cpy.srcOffset = 0;
cpy.dstOffset = offset;
cpy.size = size;
vkCmdCopyBuffer(cmd.GetCommandBuffer(), stagingBuffer.GetBuffer(), GetBuffer(), 1, &cpy);
}
, true);
VkFence fence = cmd.GetOrCreateFence();
context.GetQueueManager().Submit(VulkanQueueManager::Role::Transfer, cmd, fence);
vkWaitForFences(context.GetDevice(), 1, &fence, true, std::numeric_limits<uint64_t>::max());
context.GetCommandBufferPool().DeallocateCommandBuffer(cmd);
}
}
SH_RENDER_API auto VulkanBuffer::GetData() const -> std::vector<uint8_t>
{
if (buffer == VK_NULL_HANDLE)
return {};
const bool bGPUOnly = (memProperty & VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
const std::size_t size = static_cast<std::size_t>(bufferInfo.size);
std::vector<uint8_t> result;
if (dataPtr != nullptr)
{
result.resize(size);
result.assign(reinterpret_cast<uint8_t*>(dataPtr), reinterpret_cast<uint8_t*>(dataPtr) + size);
}
else
{
if (bGPUOnly)
{
VulkanBuffer temp{ context };
temp.Create(size, VkBufferUsageFlagBits::VK_BUFFER_USAGE_TRANSFER_DST_BIT, VkSharingMode::VK_SHARING_MODE_EXCLUSIVE,
VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, true);
VulkanCommandBuffer& cmd = *context.GetCommandBufferPool().AllocateCommandBuffer(std::this_thread::get_id(), VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT);
cmd.Build(
[&]()
{
VkBufferCopy cpy{};
cpy.srcOffset = 0;
cpy.dstOffset = 0;
cpy.size = size;
vkCmdCopyBuffer(cmd.GetCommandBuffer(), GetBuffer(), temp.GetBuffer(), 1, &cpy);
}
, true);
VkFence fence = cmd.GetOrCreateFence();
context.GetQueueManager().Submit(VulkanQueueManager::Role::Transfer, cmd, fence);
vkWaitForFences(context.GetDevice(), 1, &fence, true, std::numeric_limits<uint64_t>::max());
context.GetCommandBufferPool().DeallocateCommandBuffer(cmd);
return temp.GetData();
}
result.resize(size);
void* tempPtr = nullptr;
vmaMapMemory(context.GetAllocator(), bufferMem, &tempPtr);
result.assign(reinterpret_cast<uint8_t*>(tempPtr), reinterpret_cast<uint8_t*>(tempPtr) + size);
vmaUnmapMemory(context.GetAllocator(), bufferMem);
}
return result;
}
//auto VulkanBuffer::FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) -> uint32_t
//{
// VkPhysicalDeviceMemoryProperties memProperties;
// vkGetPhysicalDeviceMemoryProperties(context.GetGPU(), &memProperties);
// for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
// if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
// return i;
// }
// throw std::runtime_error("Failed to find suitable memory type!");
//}
}//namespace