Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1#pragma once
2#include <vk_mem_alloc.h>
3#include <string>
4
5namespace Engine::Assets
6{
7 class Texture;
8} // namespace Engine::Assets
9
10namespace Engine::Core
11{
13 class AssetManager;
14} // namespace Engine::Core
15
16namespace Vulkan {
17
26class Buffer{
29public:
30
31 // Move constructor
32 Buffer(Buffer&& other) noexcept
33 : device_(other.device_)
34 , physicalDevice_(other.physicalDevice_)
35 , allocator_(other.allocator_)
36 , buffer(other.buffer)
37 , allocation_(other.allocation_)
38 , size_(other.size_)
39 , mappedMemory(other.mappedMemory)
40 , isHostCoherent_(other.isHostCoherent_)
41 , isPersistentlyMapped_(other.isPersistentlyMapped_)
42 {
43 // Reset the moved-from object
44 other.device_ = VK_NULL_HANDLE;
45 other.physicalDevice_ = VK_NULL_HANDLE;
46 other.allocator_ = VK_NULL_HANDLE;
47 other.buffer = VK_NULL_HANDLE;
48 other.allocation_ = VK_NULL_HANDLE;
49 other.size_ = 0;
50 other.mappedMemory = nullptr;
51 other.isHostCoherent_ = false;
52 other.isPersistentlyMapped_ = false;
53 }
54
55 // Move assignment
56 Buffer& operator=(Buffer&& other) noexcept
57 {
58 if (this != &other)
59 {
60 // Only destroy if we actually have resources
61 if (buffer != VK_NULL_HANDLE || allocation_ != VK_NULL_HANDLE) {
62 destroy();
63 }
64
65 // Move from other
66 device_ = other.device_;
67 physicalDevice_ = other.physicalDevice_;
68 allocator_ = other.allocator_;
69 buffer = other.buffer;
70 allocation_ = other.allocation_;
71 size_ = other.size_;
72 mappedMemory = other.mappedMemory;
73 isHostCoherent_ = other.isHostCoherent_;
74 isPersistentlyMapped_ = other.isPersistentlyMapped_;
75
76 // Reset other
77 other.device_ = VK_NULL_HANDLE;
78 other.physicalDevice_ = VK_NULL_HANDLE;
79 other.allocator_ = VK_NULL_HANDLE;
80 other.buffer = VK_NULL_HANDLE;
81 other.allocation_ = VK_NULL_HANDLE;
82 other.size_ = 0;
83 other.mappedMemory = VK_NULL_HANDLE;
84 other.isHostCoherent_ = false;
85 other.isPersistentlyMapped_ = false;
86 }
87 return *this;
88 }
89
94
103 Buffer(VkDevice device, VkPhysicalDevice physicalDevice);
104
110
112
118 void setDebugName(const std::string& name);
119
120 [[nodiscard]] std::string getDebugName() const;
121
129 void create(size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
130
134 void destroy();
135
141 void* map();
142
146 void unmap();
147
153 [[nodiscard]] bool isMapped() const { return mappedMemory != VK_NULL_HANDLE; }
154
161 void flush(VkDeviceSize mappedOffset = 0, VkDeviceSize mappedSize = VK_WHOLE_SIZE);
168 void uploadData(const void* data, VkDeviceSize dataSize);
169
177 void uploadDataAtOffset(const void* data, VkDeviceSize dataSize, VkDeviceSize offset);
178
190 bool ensureSize(size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
191
192 [[nodiscard]] VkBuffer getBuffer() const { return buffer; }
193 [[nodiscard]] VkDeviceAddress getDeviceAddress() const;
198 [[nodiscard]] VkDeviceMemory getBufferMemory() const;
199 [[nodiscard]] VkDeviceSize getBufferSize() const { return size_; }
200
201 [[nodiscard]] void* getMappedMemory() const { return mappedMemory; }
202 [[nodiscard]] bool isValid() const { return buffer != VK_NULL_HANDLE; }
204 {
205 buffer = VK_NULL_HANDLE;
206 allocation_ = VK_NULL_HANDLE;
207 size_ = 0;
208 mappedMemory = VK_NULL_HANDLE;
209 isHostCoherent_ = false;
210 isPersistentlyMapped_ = false;
211 }
212
221 void overrideSize(VkDeviceSize newSize) { size_ = newSize; }
222
223private:
224 std::string debugName_ = "Buffer";
225
226 VkDevice device_ = VK_NULL_HANDLE;
227 VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE;
228 VmaAllocator allocator_ = VK_NULL_HANDLE;
229
230 VkBuffer buffer = VK_NULL_HANDLE;
231 VmaAllocation allocation_ = VK_NULL_HANDLE;
232 VkDeviceSize size_ = 0;
233
234 void* mappedMemory = VK_NULL_HANDLE;
235
237 bool isHostCoherent_ = false;
238
242};
243}
Wrapper for texture data.
The application context is the core class which stores the basic openxr and vulkan objects.
void * getMappedMemory() const
Definition Buffer.h:201
void flush(VkDeviceSize mappedOffset=0, VkDeviceSize mappedSize=VK_WHOLE_SIZE)
Flushes a mapped memory range to the GPU to make CPU writes visible.
Buffer(VkDevice device, VkPhysicalDevice physicalDevice)
Construct with direct Vulkan handles (decoupled from ApplicationContext)
VmaAllocator allocator_
Borrowed reference to VMA allocator.
Definition Buffer.h:228
void unmap()
Unmaps the memory if it is mapped.
void * mappedMemory
Definition Buffer.h:234
Buffer & operator=(Buffer &&other) noexcept
Definition Buffer.h:56
VkDeviceSize getBufferSize() const
Definition Buffer.h:199
void uploadDataAtOffset(const void *data, VkDeviceSize dataSize, VkDeviceSize offset)
Helper function to map, copy data at an offset and unmap the buffer.
void setDebugName(const std::string &name)
Sets the name shown in vulkan validation layer.
VmaAllocation allocation_
VMA allocation handle.
Definition Buffer.h:231
void invalidate()
Definition Buffer.h:203
VkBuffer buffer
Definition Buffer.h:230
VkDevice device_
Definition Buffer.h:226
void create(size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Creates a buffer with a certain size, usage flags and memory properties.
Buffer(Buffer &&other) noexcept
Definition Buffer.h:32
VkDeviceSize size_
Definition Buffer.h:232
bool isMapped() const
Checks whether the buffer is already mapped to a memory range.
Definition Buffer.h:153
std::string getDebugName() const
Buffer(Engine::Core::ApplicationContext *context)
Construct from ApplicationContext (for engine code)
Buffer()
Default constructor - uses EngineManager singleton to get Vulkan handles.
void uploadData(const void *data, VkDeviceSize dataSize)
Helper function to map, copy the data and unmap the buffer. Best used for infrequent updates.
bool isHostCoherent_
True if buffer uses HOST_COHERENT memory (no flush needed)
Definition Buffer.h:237
VkBuffer getBuffer() const
Definition Buffer.h:192
void destroy()
Destroys all vulkan resources of the buffer.
void * map()
Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.
void overrideSize(VkDeviceSize newSize)
Overrides the size of a buffer in case the VulkanBuffer is created in place.
Definition Buffer.h:221
bool isValid() const
Definition Buffer.h:202
VkDeviceAddress getDeviceAddress() const
VkPhysicalDevice physicalDevice_
Definition Buffer.h:227
VkDeviceMemory getBufferMemory() const
Gets the underlying VkDeviceMemory handle (for backward compatibility)
bool ensureSize(size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Ensures the buffer has at least the required size, recreating if necessary.
bool isPersistentlyMapped_
Definition Buffer.h:241
std::string debugName_
Definition Buffer.h:224
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9