2#include <vulkan/vulkan.h>
15 VkDeviceMemory
memory = VK_NULL_HANDLE;
36 VkBufferUsageFlags usage,
37 VkMemoryPropertyFlags properties)
68 *
this = std::move(other);
83 other.buffer = VK_NULL_HANDLE;
84 other.memory = VK_NULL_HANDLE;
99 VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
104 VkBufferCreateInfo bufferInfo{};
105 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
107 bufferInfo.usage = usage;
108 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
110 if (vkCreateBuffer(dev, &bufferInfo,
nullptr, &
buffer) != VK_SUCCESS)
111 throw std::runtime_error(
"Failed to create Vulkan buffer!");
113 VkMemoryRequirements memReq;
114 vkGetBufferMemoryRequirements(dev,
buffer, &memReq);
116 VkMemoryAllocateInfo allocInfo{};
117 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
118 allocInfo.allocationSize = memReq.size;
119 allocInfo.memoryTypeIndex =
findMemoryType(phys, memReq.memoryTypeBits, properties);
121 if (vkAllocateMemory(dev, &allocInfo,
nullptr, &
memory) != VK_SUCCESS)
122 throw std::runtime_error(
"Failed to allocate Vulkan buffer memory!");
134 void upload(
const void* srcData, VkDeviceSize dataSize, VkDeviceSize
offset = 0) {
135 if (dataSize == 0)
return;
136 if (!
memory)
throw std::runtime_error(
"Buffer memory not allocated!");
139 std::memcpy(dst, srcData,
static_cast<size_t>(dataSize));
175 static uint32_t
findMemoryType(VkPhysicalDevice phys, uint32_t typeFilter, VkMemoryPropertyFlags properties) {
176 VkPhysicalDeviceMemoryProperties memProps;
177 vkGetPhysicalDeviceMemoryProperties(phys, &memProps);
179 for (uint32_t i = 0; i < memProps.memoryTypeCount; i++) {
180 if ((typeFilter & (1 << i)) &&
181 (memProps.memoryTypes[i].propertyFlags & properties) == properties)
185 throw std::runtime_error(
"Failed to find suitable memory type!");
VkBuffer get() const
Gets raw Vulkan buffer.
Definition Buffer.hpp:165
VkDeviceMemory memory
Vulkan device memory handle.
Definition Buffer.hpp:15
void destroy()
Frees the buffer and associated memory.
Definition Buffer.hpp:147
VkDeviceSize size
Size of the buffer in bytes.
Definition Buffer.hpp:19
~VulkanBuffer()
Destroy the Vulkan Buffer object and releases Vulkan handles.
Definition Buffer.hpp:56
VulkanBuffer & operator=(VulkanBuffer &&other) noexcept
Move assignment operator.
Definition Buffer.hpp:76
VulkanBuffer(const VulkanBuffer &)=delete
VkDevice device
Reference to logical device.
Definition Buffer.hpp:17
VulkanBuffer(VulkanBuffer &&other) noexcept
Move constructor.
Definition Buffer.hpp:67
static uint32_t findMemoryType(VkPhysicalDevice phys, uint32_t typeFilter, VkMemoryPropertyFlags properties)
Locates the correct memory type on physical device.
Definition Buffer.hpp:175
void upload(const void *srcData, VkDeviceSize dataSize, VkDeviceSize offset=0)
Uploads data to host-visible memory in the buffer.
Definition Buffer.hpp:134
VulkanBuffer(VkDevice dev, VkPhysicalDevice phys, VkDeviceSize bufferSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Construct and initialize a new Vulkan Buffer object.
Definition Buffer.hpp:34
VkDeviceSize bufferSize
Size allocated in bytes.
Definition VulkanBuffer.hpp:85
VulkanBuffer(VkDevice dev, VkBuffer buf, VkDeviceMemory mem)
Construct a Vulkan Buffer object wrapping existing Vulkan resources.
Definition Buffer.hpp:49
void create(VkDevice dev, VkPhysicalDevice phys, VkDeviceSize bufferSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Creates and allocates the Vulkan buffer.
Definition Buffer.hpp:98
VulkanBuffer()=default
Default constructor.
VkBuffer buffer
Vulkan buffer handle.
Definition Buffer.hpp:13
VulkanBuffer & operator=(const VulkanBuffer &)=delete
uint64_t offset
Definition ufbx.cpp:9361