Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1#pragma once
2#include <vulkan/vulkan.h>
3#include <stdexcept>
4#include <cstring>
5
11public:
13 VkBuffer buffer = VK_NULL_HANDLE;
15 VkDeviceMemory memory = VK_NULL_HANDLE;
17 VkDevice device = VK_NULL_HANDLE;
19 VkDeviceSize size = 0;
20
24 VulkanBuffer() = default;
25
34 VulkanBuffer(VkDevice dev, VkPhysicalDevice phys,
35 VkDeviceSize bufferSize,
36 VkBufferUsageFlags usage,
37 VkMemoryPropertyFlags properties)
38 : device(dev), size(bufferSize)
39 {
40 create(dev, phys, bufferSize, usage, properties);
41 }
42
49 VulkanBuffer(VkDevice dev, VkBuffer buf, VkDeviceMemory mem)
50 : device(dev), buffer(buf), memory(mem) {
51 }
52
57 destroy();
58 }
59
60 VulkanBuffer(const VulkanBuffer&) = delete;
62
67 VulkanBuffer(VulkanBuffer&& other) noexcept {
68 *this = std::move(other);
69 }
70
76 VulkanBuffer& operator=(VulkanBuffer&& other) noexcept {
77 if (this != &other) {
78 destroy();
79 device = other.device;
80 buffer = other.buffer;
81 memory = other.memory;
82 size = other.size;
83 other.buffer = VK_NULL_HANDLE;
84 other.memory = VK_NULL_HANDLE;
85 }
86 return *this;
87 }
88
89 // --- Create & allocate ---
98 void create(VkDevice dev, VkPhysicalDevice phys, VkDeviceSize bufferSize,
99 VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
100 {
101 device = dev;
103
104 VkBufferCreateInfo bufferInfo{};
105 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
106 bufferInfo.size = bufferSize;
107 bufferInfo.usage = usage;
108 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
109
110 if (vkCreateBuffer(dev, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
111 throw std::runtime_error("Failed to create Vulkan buffer!");
112
113 VkMemoryRequirements memReq;
114 vkGetBufferMemoryRequirements(dev, buffer, &memReq);
115
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);
120
121 if (vkAllocateMemory(dev, &allocInfo, nullptr, &memory) != VK_SUCCESS)
122 throw std::runtime_error("Failed to allocate Vulkan buffer memory!");
123
124 vkBindBufferMemory(dev, buffer, memory, 0);
125 }
126
127 // --- Write data to the buffer (host visible) ---
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!");
137 void* dst;
138 vkMapMemory(device, memory, offset, dataSize, 0, &dst);
139 std::memcpy(dst, srcData, static_cast<size_t>(dataSize));
140 vkUnmapMemory(device, memory);
141 }
142
143 // --- Destroy safely ---
147 void destroy() {
148 if (buffer) {
149 if (device) {
150 vkDeviceWaitIdle(device);
151 }
152 vkDestroyBuffer(device, buffer, nullptr);
153 buffer = VK_NULL_HANDLE;
154 }
155 if (memory) {
156 vkFreeMemory(device, memory, nullptr);
157 memory = VK_NULL_HANDLE;
158 }
159 }
160
165 VkBuffer get() const { return buffer; }
166
167private:
175 static uint32_t findMemoryType(VkPhysicalDevice phys, uint32_t typeFilter, VkMemoryPropertyFlags properties) {
176 VkPhysicalDeviceMemoryProperties memProps;
177 vkGetPhysicalDeviceMemoryProperties(phys, &memProps);
178
179 for (uint32_t i = 0; i < memProps.memoryTypeCount; i++) {
180 if ((typeFilter & (1 << i)) &&
181 (memProps.memoryTypes[i].propertyFlags & properties) == properties)
182 return i;
183 }
184
185 throw std::runtime_error("Failed to find suitable memory type!");
186 }
187};
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