vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1///
2/// @file Buffer.hpp
3/// @brief This file contains the Buffer class
4/// @namespace ven
5///
6
7#pragma once
8
9#include "VEngine/Device.hpp"
10
11namespace ven {
12
13 ///
14 /// @class Buffer
15 /// @brief Class for buffer
16 /// @namespace ven
17 class Buffer {
18
19 public:
20
21 Buffer(Device& device, VkDeviceSize instanceSize, uint32_t instanceCount, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize minOffsetAlignment = 1);
22 ~Buffer();
23
24 Buffer(const Buffer&) = delete;
25 Buffer& operator=(const Buffer&) = delete;
26
27 ///
28 /// @brief Map a memory range of this buffer. If successful, mapped points to the specified buffer range.
29 ///
30 /// @param size (Optional) Size of the memory range to map. Pass VK_WHOLE_SIZE to map the complete buffer range.
31 /// @param offset (Optional) Byte offset from beginning
32 ///
33 /// @return VkResult of the buffer mapping call
34 ///
35 VkResult map(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
36
37 ///
38 /// @brief Unmap a mapped memory range
39 ///
40 /// @note Does not return a result as vkUnmapMemory can't fail
41 ///
42 void unmap();
43
44 ///
45 /// @brief Copies the specified data to the mapped buffer. Default value writes whole buffer range
46 ///
47 /// @param data Pointer to the data to copy
48 /// @param size (Optional) Size of the data to copy. Pass VK_WHOLE_SIZE to flush the complete buffer range.
49 /// @param offset (Optional) Byte offset from beginning of mapped region
50 ///
51 void writeToBuffer(const void* data, VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0) const;
52
53 ///
54 /// @brief Flush a memory range of the buffer to make it visible to the device
55 ///
56 /// @note Only required for non-coherent memory
57 ///
58 /// @param size (Optional) Size of the memory range to flush. Pass VK_WHOLE_SIZE to flush the complete buffer range.
59 /// @param offset (Optional) Byte offset from beginning
60 ///
61 /// @return VkResult of the flush call
62 ///
63 [[nodiscard]] VkResult flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0) const;
64
65 ///
66 /// @brief Create a buffer info descriptor
67 ///
68 /// @param size (Optional) Size of the memory range of the descriptor
69 /// @param offset (Optional) Byte offset from beginning
70 ///
71 /// @return VkDescriptorBufferInfo of specified offset and range
72 ///
73 [[nodiscard]] VkDescriptorBufferInfo descriptorInfo(const VkDeviceSize size = VK_WHOLE_SIZE, const VkDeviceSize offset = 0) const { return VkDescriptorBufferInfo{m_buffer, offset, size, }; }
74
75 ///
76 /// @brief Invalidate a memory range of the buffer to make it visible to the host
77 ///
78 /// @note Only required for non-coherent memory
79 ///
80 /// @param size (Optional) Size of the memory range to invalidate. Pass VK_WHOLE_SIZE to invalidate
81 /// the complete buffer range.
82 /// @param offset (Optional) Byte offset from beginning
83 ///
84 /// @return VkResult of the invalidate call
85 ///
86 [[nodiscard]] VkResult invalidate(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0) const;
87
88 ///
89 /// Copies "instanceSize" bytes of data to the mapped buffer at an offset of index * alignmentSize
90 ///
91 /// @param data Pointer to the data to copy
92 /// @param index Used in offset calculation
93 ///
94 ///
95 void writeToIndex(const void* data, const VkDeviceSize index) const { writeToBuffer(data, m_instanceSize, index * m_alignmentSize); }
96
97 ///
98 /// Flush the memory range at index * alignmentSize of the buffer to make it visible to the device
99 ///
100 /// @param index Used in offset calculation
101 ///
102 [[nodiscard]] VkResult flushIndex(const VkDeviceSize index) const { return flush(m_alignmentSize, index * m_alignmentSize); }
103
104 ///
105 ///
106 /// Create a buffer info descriptor
107 ///
108 /// @param index Specifies the region given by index * alignmentSize
109 ///
110 /// @return VkDescriptorBufferInfo for instance at index
111 ///
112 [[nodiscard]] VkDescriptorBufferInfo descriptorInfoForIndex(const VkDeviceSize index) const { return descriptorInfo(m_alignmentSize, index * m_alignmentSize); }
113
114 ///
115 /// Invalidate a memory range of the buffer to make it visible to the host
116 ///
117 /// @note Only required for non-coherent memory
118 ///
119 /// @param index Specifies the region to invalidate: index * alignmentSize
120 ///
121 /// @return VkResult of the invalidate call
122 ///
123 [[nodiscard]] VkResult invalidateIndex(const VkDeviceSize index) const { return invalidate(m_alignmentSize, index * m_alignmentSize); }
124
125 [[nodiscard]] VkBuffer getBuffer() const { return m_buffer; }
126 [[nodiscard]] void* getMappedMemory() const { return m_mapped; }
127 [[nodiscard]] uint32_t getInstanceCount() const { return m_instanceCount; }
128 [[nodiscard]] VkDeviceSize getInstanceSize() const { return m_instanceSize; }
129 [[nodiscard]] VkDeviceSize getAlignmentSize() const { return m_instanceSize; }
130 [[nodiscard]] VkBufferUsageFlags getUsageFlags() const { return m_usageFlags; }
131 [[nodiscard]] VkMemoryPropertyFlags getMemoryPropertyFlags() const { return m_memoryPropertyFlags; }
132 [[nodiscard]] VkDeviceSize getBufferSize() const { return m_bufferSize; }
133
134 private:
135 ///
136 /// Returns the minimum instance size required to be compatible with devices minOffsetAlignment
137 ///
138 /// @param instanceSize The size of an instance
139 /// @param minOffsetAlignment The minimum required alignment, in bytes, for the offset member (eg
140 /// minUniformBufferOffsetAlignment)
141 ///
142 /// @return VkResult of the buffer mapping call
143 ///
144 static VkDeviceSize getAlignment(VkDeviceSize instanceSize, VkDeviceSize minOffsetAlignment);
145
147 void* m_mapped = nullptr;
148 VkBuffer m_buffer = VK_NULL_HANDLE;
149 VkDeviceMemory m_memory = VK_NULL_HANDLE;
150
151 VkDeviceSize m_bufferSize;
152 VkDeviceSize m_instanceSize;
154 VkDeviceSize m_alignmentSize;
155 VkBufferUsageFlags m_usageFlags;
156 VkMemoryPropertyFlags m_memoryPropertyFlags;
157
158 }; // class Buffer
159
160} // namespace ven
This file contains the Device class.
Class for buffer.
Definition Buffer.hpp:17
VkBuffer getBuffer() const
Definition Buffer.hpp:125
void unmap()
Unmap a mapped memory range.
Definition buffer.cpp:32
void * getMappedMemory() const
Definition Buffer.hpp:126
uint32_t m_instanceCount
Definition Buffer.hpp:153
VkDeviceSize m_bufferSize
Definition Buffer.hpp:151
VkBuffer m_buffer
Definition Buffer.hpp:148
VkDeviceSize getBufferSize() const
Definition Buffer.hpp:132
VkResult map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Map a memory range of this buffer.
Definition buffer.cpp:26
void * m_mapped
Definition Buffer.hpp:147
VkResult flush(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Flush a memory range of the buffer to make it visible to the device.
Definition buffer.cpp:53
VkDeviceSize m_alignmentSize
Definition Buffer.hpp:154
Buffer & operator=(const Buffer &)=delete
VkDeviceMemory m_memory
Definition Buffer.hpp:149
VkResult invalidateIndex(const VkDeviceSize index) const
Invalidate a memory range of the buffer to make it visible to the host.
Definition Buffer.hpp:123
VkMemoryPropertyFlags getMemoryPropertyFlags() const
Definition Buffer.hpp:131
VkResult invalidate(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Invalidate a memory range of the buffer to make it visible to the host.
Definition buffer.cpp:63
VkDeviceSize getAlignmentSize() const
Definition Buffer.hpp:129
VkDescriptorBufferInfo descriptorInfoForIndex(const VkDeviceSize index) const
Create a buffer info descriptor.
Definition Buffer.hpp:112
Device & m_device
Definition Buffer.hpp:146
uint32_t getInstanceCount() const
Definition Buffer.hpp:127
static VkDeviceSize getAlignment(VkDeviceSize instanceSize, VkDeviceSize minOffsetAlignment)
Returns the minimum instance size required to be compatible with devices minOffsetAlignment.
Definition buffer.cpp:6
Buffer(Device &device, VkDeviceSize instanceSize, uint32_t instanceCount, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize minOffsetAlignment=1)
Definition buffer.cpp:13
Buffer(const Buffer &)=delete
VkDescriptorBufferInfo descriptorInfo(const VkDeviceSize size=VK_WHOLE_SIZE, const VkDeviceSize offset=0) const
Create a buffer info descriptor.
Definition Buffer.hpp:73
VkResult flushIndex(const VkDeviceSize index) const
Flush the memory range at index * alignmentSize of the buffer to make it visible to the device.
Definition Buffer.hpp:102
VkMemoryPropertyFlags m_memoryPropertyFlags
Definition Buffer.hpp:156
void writeToBuffer(const void *data, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Copies the specified data to the mapped buffer.
Definition buffer.cpp:40
VkDeviceSize m_instanceSize
Definition Buffer.hpp:152
VkBufferUsageFlags getUsageFlags() const
Definition Buffer.hpp:130
void writeToIndex(const void *data, const VkDeviceSize index) const
Copies "instanceSize" bytes of data to the mapped buffer at an offset of index * alignmentSize.
Definition Buffer.hpp:95
VkDeviceSize getInstanceSize() const
Definition Buffer.hpp:128
VkBufferUsageFlags m_usageFlags
Definition Buffer.hpp:155