6 VulkanBuffer::VulkanBuffer(VulkanLogicalDevice * device, VmaAllocator allocator, uint32_t size, VkBufferUsageFlags usage,
bool gpu, VkCommandPool pool)
9 this->allocator = allocator;
15 VkBufferCreateInfo createInfo = {};
16 createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
17 createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
18 createInfo.size = size;
19 createInfo.usage = usage;
20 createInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
22 VmaAllocationCreateInfo allocInfo = {};
24 allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
26 allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
28 vmaCreateBuffer(allocator, &createInfo, &allocInfo, &buffer, &allocation, &allocationInfo);
32 VulkanBuffer::VulkanBuffer(VulkanLogicalDevice * device, VmaAllocator allocator, uint32_t size, VkBufferUsageFlags usage, VmaMemoryUsage memoryType, VkCommandPool pool)
34 this->device = device;
35 this->allocator = allocator;
41 VkBufferCreateInfo createInfo = {};
42 createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
43 createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
44 createInfo.size = size;
45 createInfo.usage = usage;
46 createInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
48 VmaAllocationCreateInfo allocInfo = {};
49 allocInfo.usage = memoryType;
51 if (memoryType == VMA_MEMORY_USAGE_GPU_ONLY)
56 vmaCreateBuffer(allocator, &createInfo, &allocInfo, &buffer, &allocation, &allocationInfo);
59 VulkanBuffer::~VulkanBuffer()
61 vkQueueWaitIdle(device->GetGraphicsQueue());
62 vmaDestroyBuffer(allocator, buffer, allocation);
65 void VulkanBuffer::CreateBufferView(uint32_t range, uint32_t offset, VkFormat format)
67 VkBufferViewCreateInfo viewInfo = {};
68 viewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
69 viewInfo.buffer = buffer;
70 viewInfo.range = range;
71 viewInfo.offset = offset;
72 viewInfo.format = format;
75 vkCreateBufferView(device->GetDevice(), &viewInfo,
nullptr, &view);
79 VkBuffer VulkanBuffer::GetBuffer()
const 84 VkBufferView VulkanBuffer::GetBufferView()
const 89 VmaAllocationInfo VulkanBuffer::getAllocationInfo()
91 return allocationInfo;
94 void VulkanBuffer::UpdateBuffer(
void * data, uint32_t offset, uint32_t size)
97 VkBufferCreateInfo createInfo = {};
98 createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
99 createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
100 createInfo.size = size;
101 createInfo.usage = (usage | VK_BUFFER_USAGE_TRANSFER_SRC_BIT) & (~VK_BUFFER_USAGE_TRANSFER_DST_BIT);
103 VmaAllocationCreateInfo allocInfo = {};
104 allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
106 VkBuffer stagingBuffer;
107 VmaAllocation stagingAllocation;
109 vmaCreateBuffer(allocator, &createInfo, &allocInfo, &stagingBuffer, &stagingAllocation,
nullptr);
112 vmaMapMemory(allocator, stagingAllocation, &dst);
113 memcpy((static_cast<char*>(dst) + offset), data, size);
114 vmaUnmapMemory(allocator, stagingAllocation);
116 VkCommandBufferAllocateInfo cmdAllocInfo = {};
117 cmdAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
118 cmdAllocInfo.commandBufferCount = 1;
119 cmdAllocInfo.commandPool = pool;
120 cmdAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
122 VkCommandBuffer cmdBuffer;
124 vkAllocateCommandBuffers(device->GetDevice(), &cmdAllocInfo, &cmdBuffer);
126 VkCommandBufferBeginInfo cmdBeginInfo = {};
127 cmdBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
128 cmdBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
130 vkBeginCommandBuffer(cmdBuffer, &cmdBeginInfo);
132 VkBufferCopy copyRegion = {};
133 copyRegion.srcOffset = offset;
134 copyRegion.dstOffset = offset;
135 copyRegion.size = size;
136 vkCmdCopyBuffer(cmdBuffer, stagingBuffer, buffer, 1, ©Region);
138 vkEndCommandBuffer(cmdBuffer);
140 VkSubmitInfo submitInfo = {};
141 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
142 submitInfo.commandBufferCount = 1;
143 submitInfo.pCommandBuffers = &cmdBuffer;
145 vkQueueSubmit(device->GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
146 vkQueueWaitIdle(device->GetGraphicsQueue());
148 vkFreeCommandBuffers(device->GetDevice(), pool, 1, &cmdBuffer);
150 vmaDestroyBuffer(allocator, stagingBuffer, stagingAllocation);
155 vmaMapMemory(allocator, allocation, &dst);
156 memcpy((static_cast<char*>(dst) + offset), data, size);
157 vmaUnmapMemory(allocator, allocation);
161 void VulkanBuffer::MapBuffer(
void * location)
163 vmaMapMemory(allocator, allocation, &location);
166 void VulkanBuffer::UnmapBuffer()
168 vmaUnmapMemory(allocator, allocation);
171 void VulkanBuffer::ClearBuffer()
const 173 vkQueueWaitIdle(device->GetGraphicsQueue());
175 VkCommandBufferAllocateInfo cmdAllocInfo = {};
176 cmdAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
177 cmdAllocInfo.commandBufferCount = 1;
178 cmdAllocInfo.commandPool = pool;
179 cmdAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
181 VkCommandBuffer cmdBuffer;
183 vkAllocateCommandBuffers(device->GetDevice(), &cmdAllocInfo, &cmdBuffer);
185 VkCommandBufferBeginInfo cmdBeginInfo = {};
186 cmdBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
187 cmdBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
189 vkBeginCommandBuffer(cmdBuffer, &cmdBeginInfo);
191 vkCmdFillBuffer(cmdBuffer, buffer, 0, VK_WHOLE_SIZE, 0);
193 vkEndCommandBuffer(cmdBuffer);
195 VkSubmitInfo submitInfo = {};
196 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
197 submitInfo.commandBufferCount = 1;
198 submitInfo.pCommandBuffers = &cmdBuffer;
200 vkQueueSubmit(device->GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
201 vkQueueWaitIdle(device->GetGraphicsQueue());
203 vkFreeCommandBuffers(device->GetDevice(), pool, 1, &cmdBuffer);
207 #endif // USING_VULKAN