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