Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
VulkanBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vulkan/vulkan.h>
4#include <vk_mem_alloc.h>
5#include <string>
6
7namespace EngineCore
8{
9class Texture;
10}
11namespace EngineCore
12{
13class AssetManager;
15}
16namespace EngineCore {
17
32public:
33
34 // Move constructor
35 VulkanBuffer(VulkanBuffer&& other) noexcept
36 : device_(other.device_)
37 , physicalDevice_(other.physicalDevice_)
38 , allocator_(other.allocator_)
39 , buffer(other.buffer)
40 , allocation_(other.allocation_)
41 , size(other.size)
42 , mappedMemory(other.mappedMemory)
43 , isHostCoherent_(other.isHostCoherent_)
44 , isPersistentlyMapped_(other.isPersistentlyMapped_)
45 {
46 // Reset the moved-from object
47 other.device_ = VK_NULL_HANDLE;
48 other.physicalDevice_ = VK_NULL_HANDLE;
49 other.allocator_ = VK_NULL_HANDLE;
50 other.buffer = VK_NULL_HANDLE;
51 other.allocation_ = VK_NULL_HANDLE;
52 other.size = 0;
53 other.mappedMemory = nullptr;
54 other.isHostCoherent_ = false;
55 other.isPersistentlyMapped_ = false;
56 }
57
58 // Move assignment
60 {
61 if (this != &other)
62 {
63 // Only destroy if we actually have resources
64 if (buffer != VK_NULL_HANDLE || allocation_ != VK_NULL_HANDLE) {
65 destroy();
66 }
67
68 // Move from other
69 device_ = other.device_;
70 physicalDevice_ = other.physicalDevice_;
71 allocator_ = other.allocator_;
72 buffer = other.buffer;
73 allocation_ = other.allocation_;
74 size = other.size;
75 mappedMemory = other.mappedMemory;
76 isHostCoherent_ = other.isHostCoherent_;
77 isPersistentlyMapped_ = other.isPersistentlyMapped_;
78
79 // Reset other
80 other.device_ = VK_NULL_HANDLE;
81 other.physicalDevice_ = VK_NULL_HANDLE;
82 other.allocator_ = VK_NULL_HANDLE;
83 other.buffer = VK_NULL_HANDLE;
84 other.allocation_ = VK_NULL_HANDLE;
85 other.size = 0;
86 other.mappedMemory = VK_NULL_HANDLE;
87 other.isHostCoherent_ = false;
88 other.isPersistentlyMapped_ = false;
89 }
90 return *this;
91 }
92
97
109 VulkanBuffer(VkDevice device, VkPhysicalDevice physicalDevice);
110
115 explicit VulkanBuffer(ApplicationContext* context);
116
118
127 void setDebugName(const std::string& name);
128
129 [[nodiscard]] std::string getDebugName() const;
130
141 void create(size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
142
149 void destroy();
150
159 void* map();
160
167 void unmap();
168
177 [[nodiscard]] bool isMapped() const { return mappedMemory != VK_NULL_HANDLE; }
178
188 void flush(VkDeviceSize mappedOffset = 0, VkDeviceSize mappedSize = VK_WHOLE_SIZE);
198 void uploadData(const void* data, VkDeviceSize dataSize);
199
214 bool ensureSize(size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
215
216 [[nodiscard]] VkBuffer getBuffer() const { return buffer; }
221 [[nodiscard]] VkDeviceMemory getBufferMemory() const;
222 [[nodiscard]] VkDeviceSize getBufferSize() const { return size; }
223
224 [[nodiscard]] void* getMappedMemory() const { return mappedMemory; }
225 [[nodiscard]] bool isValid() const { return buffer != VK_NULL_HANDLE; }
227 {
228 buffer = VK_NULL_HANDLE;
229 allocation_ = VK_NULL_HANDLE;
230 size = 0;
231 mappedMemory = VK_NULL_HANDLE;
232 isHostCoherent_ = false;
233 isPersistentlyMapped_ = false;
234 }
235
244 void overrideSize(VkDeviceSize newSize) { size = newSize; }
245
246private:
247 std::string debugName = "Buffer";
248
249 VkDevice device_ = VK_NULL_HANDLE;
250 VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE;
251 VmaAllocator allocator_ = VK_NULL_HANDLE;
252
253 VkBuffer buffer = VK_NULL_HANDLE;
254 VmaAllocation allocation_ = VK_NULL_HANDLE;
255 VkDeviceSize size = 0;
256
257 void* mappedMemory = VK_NULL_HANDLE;
258
260 bool isHostCoherent_ = false;
261
265};
266}
The application context is the core class which stores the basic openxr and vulkan objects.
void setDebugName(const std::string &name)
Sets the name shown in vulkan validation layer.
VkDeviceSize getBufferSize() const
VkBuffer getBuffer() const
void flush(VkDeviceSize mappedOffset=0, VkDeviceSize mappedSize=VK_WHOLE_SIZE)
Flushes a mapped memory range to the GPU to make CPU writes visible.
void * map()
Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.
VmaAllocator allocator_
Borrowed reference to VMA allocator.
void unmap()
Unmaps the memory if it is mapped.
void uploadData(const void *data, VkDeviceSize dataSize)
Helper function to map, copy the data and unmap the buffer. Best used for infrequent updates.
VulkanBuffer()
Default constructor - uses EngineManager singleton to get Vulkan handles.
VmaAllocation allocation_
VMA allocation handle.
bool ensureSize(size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Ensures the buffer has at least the required size, recreating if necessary.
bool isMapped() const
Checks whether the buffer is already mapped to a memory range.
std::string getDebugName() const
void destroy()
Destroys all vulkan resources of the buffer.
void overrideSize(VkDeviceSize newSize)
Overrides the size of a buffer in case the VulkanBuffer is created in place.
VkDeviceMemory getBufferMemory() const
Gets the underlying VkDeviceMemory handle (for backward compatibility)
bool isHostCoherent_
True if buffer uses HOST_COHERENT memory (no flush needed)
VulkanBuffer & operator=(VulkanBuffer &&other) noexcept
void create(size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Creates a buffer with a certain size, usage flags and memory properties.
VkPhysicalDevice physicalDevice_
void * getMappedMemory() const
VulkanBuffer(VulkanBuffer &&other) noexcept
Log category system implementation.