2
3
4
5
16 const std::string& name ,
18 VkBufferUsageFlags usage ,
19 VkMemoryPropertyFlags properties
30
31
32 CreateBuffer(vulkanState, name, size, usage, properties);
42
43
44 vmaDestroyBuffer(m_VulkanState.m_VmaAllocator, m_Buffer, m_Alloc);
50 vkUnmapMemory(m_VulkanState.m_Device, m_BufferMemory);
54
55
56 vkDestroyBuffer(m_VulkanState.m_Device, m_Buffer,
nullptr);
57 vkFreeMemory(m_VulkanState.m_Device, m_BufferMemory,
nullptr);
72
73
77
78
79 VkBufferCopy copyRegion{};
80 copyRegion.srcOffset = 0;
81 copyRegion.dstOffset = 0;
82 copyRegion.size = size;
85
86
87 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region);
95 if (m_Usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)
97 return m_BufferAddress;
100 SPICES_CORE_ERROR(
"This Buffer Cannot Get Address")
101 return m_BufferAddress;
110 SPICES_CORE_ERROR(
"VMA cannot enter this function.")
115
116
117 VK_CHECK(vkMapMemory(m_VulkanState.m_Device, m_BufferMemory, offset, size, 0, &m_LocalMemory));
127 m_BufferInfo.buffer = m_Buffer;
128 m_BufferInfo.offset = offset;
129 m_BufferInfo.range = size;
131 return &m_BufferInfo;
141
142
143 if (size == VK_WHOLE_SIZE)
145 VK_CHECK(vmaCopyMemoryToAllocation(m_VulkanState.m_VmaAllocator, data, m_Alloc, offset, m_DeviceSize))
149 VK_CHECK(vmaCopyMemoryToAllocation(m_VulkanState.m_VmaAllocator, data, m_Alloc, offset, size))
154 if (!m_LocalMemory){ Map(); }
157
158
159 if (size == VK_WHOLE_SIZE)
161 memcpy(m_LocalMemory, data, m_DeviceSize);
165 char* memOffset =
static_cast<
char*>(m_LocalMemory);
167 memcpy(memOffset, data, size);
181
182
183 if (size == VK_WHOLE_SIZE)
185 VK_CHECK(vmaCopyAllocationToMemory(m_VulkanState.m_VmaAllocator, m_Alloc, offset, data, m_DeviceSize))
189 VK_CHECK(vmaCopyAllocationToMemory(m_VulkanState.m_VmaAllocator, m_Alloc, offset, data, size))
194 if (!m_LocalMemory) { Map(); }
197
198
199 if (size == VK_WHOLE_SIZE)
201 memcpy(data, m_LocalMemory, m_DeviceSize);
204 char* memOffset =
static_cast<
char*>(m_LocalMemory);
206 memcpy(data, memOffset, size);
220
221
222 VK_CHECK(vmaFlushAllocation(m_VulkanState.m_VmaAllocator, m_Alloc, offset, size))
227
228
229 VkMappedMemoryRange mappedRange {};
230 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
231 mappedRange.memory = m_BufferMemory;
232 mappedRange.offset = offset;
233 mappedRange.size = size;
236
237
238 VK_CHECK(vkFlushMappedMemoryRanges(m_VulkanState.m_Device, 1, &mappedRange))
246 const std::string& name ,
248 VkBufferUsageFlags usage ,
249 VkMemoryPropertyFlags properties
257 m_Flags = properties;
260
261
262 VkBufferCreateInfo bufferInfo{};
263 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
264 bufferInfo.size = size;
265 bufferInfo.usage = usage;
266 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
271
272
273 VmaAllocationCreateInfo allocInfo{};
274 allocInfo.usage = VMA_MEMORY_USAGE_AUTO;
276 if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
278 allocInfo.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
281 if (properties & VMA_MEMORY_PROPERTY_DEDICATED_MEMORY_BIT)
283 allocInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
287
288
289 VK_CHECK(vmaCreateBuffer(vulkanState.m_VmaAllocator, &bufferInfo, &allocInfo, &m_Buffer, &m_Alloc,
nullptr))
290 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_BUFFER, (uint64_t)m_Buffer, vulkanState.m_Device, name)
292 if (usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)
295
296
297 VkBufferDeviceAddressInfo info {};
298 info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
299 info.buffer = m_Buffer;
302
303
304 m_BufferAddress = vkGetBufferDeviceAddress(m_VulkanState.m_Device, &info);
310
311
312 VK_CHECK(vkCreateBuffer(vulkanState.m_Device, &bufferInfo,
nullptr, &m_Buffer));
313 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_BUFFER, (uint64_t)m_Buffer, vulkanState.m_Device,
"Buffer")
316
317
318 VkMemoryDedicatedRequirements dedicatedRegs{};
319 dedicatedRegs.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
321 VkMemoryRequirements2 memReqs{};
322 memReqs.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
323 memReqs.pNext = &dedicatedRegs;
325 VkBufferMemoryRequirementsInfo2 bufferReqs{};
326 bufferReqs.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2;
327 bufferReqs.buffer = m_Buffer;
329 vkGetBufferMemoryRequirements2(m_VulkanState.m_Device, &bufferReqs, &memReqs);
332
333
334 VkMemoryAllocateInfo allocInfo{};
335 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
336 allocInfo.allocationSize = memReqs.memoryRequirements.size;
339
340
341 VkMemoryAllocateFlagsInfoKHR flagsInfo{};
342 flagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
343 flagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
345 if (usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)
347 allocInfo.pNext = &flagsInfo;
351
352
353 VkPhysicalDeviceMemoryProperties memProperties{};
354 vkGetPhysicalDeviceMemoryProperties(vulkanState.m_PhysicalDevice, &memProperties);
357
358
359 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
361 if (memReqs.memoryRequirements.memoryTypeBits & (1 << i) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
363 allocInfo.memoryTypeIndex = i;
369
370
371 VK_CHECK(vkAllocateMemory(vulkanState.m_Device, &allocInfo,
nullptr, &m_BufferMemory));
372 VK_CHECK(vkBindBufferMemory(vulkanState.m_Device, m_Buffer, m_BufferMemory, 0));
374 if (usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)
377
378
379 VkBufferDeviceAddressInfo info {};
380 info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
381 info.buffer = m_Buffer;
384
385
386 m_BufferAddress = vkGetBufferDeviceAddress(m_VulkanState.m_Device, &info);
#define SPICES_PROFILE_ZONE
#define VMA_ALLOCATOR
Use VMA for memory allocate.
#define VK_CHECK(expr)
Vulkan Check macro. Verify Vulkan API Effectiveness.
void CreateBuffer(VulkanState &vulkanState, const std::string &name, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Create a buffer.
VulkanBuffer(VulkanState &vulkanState, const std::string &name, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Constructor Function. Create VkBuffer.
void CopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size)
Copy data from a buffer to another.
void WriteToBuffer(const void *data, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Write data to buffer.
VkDeviceAddress & GetAddress()
Get VkBuffer Address.
void Map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Map buffer video memory to a local memory.
void WriteFromBuffer(void *data, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Write data from buffer.
VkDescriptorBufferInfo * GetBufferInfo(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Get VkDescriptorBufferInfo.
virtual ~VulkanBuffer() override
Destructor Function.
std::string m_Name
Buffer Name.
void Flush(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Flush the buffer's video memory data.
This Class is a Wrapper of VulkanBuffer.
VulkanCommandBuffer Class. This class defines the VulkanCommandBuffer behaves. This class is just a w...
VulkanState & m_VulkanState
The global VulkanState Referenced from VulkanRenderBackend.
VulkanObject(VulkanState &vulkanState)
Constructor Function. Init member variables.
VulkanObject Class. This class defines the basic behaves of VulkanObject. When we create an new Vulka...
This struct contains all Vulkan object in used global.