31
31
32
32
namespace vcl {
33
33
34
+ /* *
35
+ * @brief The VertexBuffer manages the lifetime of a bgfx::VertexBufferHandle.
36
+ *
37
+ * It provides an interface to set the vertex buffer data and bind it to the
38
+ * rendering pipeline. The vertex buffer can be used for rendering or for
39
+ * compute shaders.
40
+ *
41
+ * @note A VertexBuffer can be moved but not copied (a copy would require to
42
+ * create a new bgfx::VertexBufferHandle, that can be done only having access
43
+ * to the data). Any class that contains a VertexBuffer should implement the
44
+ * copy constructor and the copy assignment operator.
45
+ */
34
46
class VertexBuffer
35
47
{
36
48
bgfx::VertexBufferHandle mVertexBufferHandle = BGFX_INVALID_HANDLE;
37
49
bool mCompute = false ;
38
50
39
51
public:
52
+ /* *
53
+ * @brief Empty constructor.
54
+ *
55
+ * It creates an invalid VertexBuffer object.
56
+ */
40
57
VertexBuffer () = default ;
41
58
59
+ // Copying a VertexBuffer is not allowed
42
60
VertexBuffer (const VertexBuffer& other) = delete ;
43
61
62
+ /* *
63
+ * @brief Move constructor.
64
+ *
65
+ * The other VertexBuffer is left in an invalid state.
66
+ *
67
+ * @param[in] other: the other VertexBuffer object.
68
+ */
44
69
VertexBuffer (VertexBuffer&& other) noexcept
45
70
{
46
- mVertexBufferHandle = other.mVertexBufferHandle ;
47
- mCompute = other.mCompute ;
48
- other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
71
+ swap (other);
49
72
}
50
73
74
+ /* *
75
+ * @brief Destructor.
76
+ *
77
+ * If the VertexBuffer is valid, the bgfx::VertexBufferHandle is destroyed.
78
+ */
51
79
~VertexBuffer ()
52
80
{
53
81
if (bgfx::isValid (mVertexBufferHandle ))
54
82
bgfx::destroy (mVertexBufferHandle );
55
83
}
56
84
85
+ // Copying a VertexBuffer is not allowed
57
86
VertexBuffer& operator =(const VertexBuffer& other) = delete ;
58
87
88
+ /* *
89
+ * @brief Move assignment operator.
90
+ *
91
+ * The other VertexBuffer is left in an invalid state.
92
+ *
93
+ * @param[in] other: the other VertexBuffer object.
94
+ * @return a reference to this object.
95
+ */
59
96
VertexBuffer& operator =(VertexBuffer&& other) noexcept
60
97
{
61
- mVertexBufferHandle = other.mVertexBufferHandle ;
62
- mCompute = other.mCompute ;
63
- other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
98
+ swap (other);
64
99
return *this ;
65
100
}
66
101
102
+ /* *
103
+ * @brief Swap the content of this object with another VertexBuffer object.
104
+ *
105
+ * @param[in] other: the other VertexBuffer object.
106
+ */
67
107
void swap (VertexBuffer& other)
68
108
{
69
109
using std::swap;
@@ -73,6 +113,38 @@ class VertexBuffer
73
113
74
114
friend void swap (VertexBuffer& a, VertexBuffer& b) { a.swap (b); }
75
115
116
+ /* *
117
+ * @brief Check if the VertexBuffer is valid.
118
+ *
119
+ * @return true if the VertexBuffer is valid, false otherwise.
120
+ */
121
+ bool isValid () const { return bgfx::isValid (mVertexBufferHandle ); }
122
+
123
+ /* *
124
+ * @brief Check if the VertexBuffer is used for compute shaders.
125
+ *
126
+ * @return true if the VertexBuffer is used for compute shaders, false
127
+ * otherwise.
128
+ */
129
+ bool isCompute () const { return mCompute ; }
130
+
131
+ /* *
132
+ * @brief Set the vertex buffer data for rendering.
133
+ *
134
+ * @note The data must be available for two bgfx::frame calls, then it is
135
+ * safe to release the data. If you cannot guarantee this, you must provide
136
+ * a release function that will be called automatically when the data is no
137
+ * longer needed.
138
+ *
139
+ * @param[in] bufferData: the data to be copied in the vertex buffer.
140
+ * @param[in] bufferSize: the size of the bufferData.
141
+ * @param[in] attrib: the attribute to which the data refers.
142
+ * @param[in] numElements: the number of elements for each vertex.
143
+ * @param[in] type: the type of the elements.
144
+ * @param[in] normalize: if true, the data is normalized.
145
+ * @param[in] releaseFn: the release function to be called when the data is
146
+ * no longer needed.
147
+ */
76
148
void set (
77
149
const void * bufferData,
78
150
const uint bufferSize,
@@ -92,6 +164,24 @@ class VertexBuffer
92
164
releaseFn));
93
165
}
94
166
167
+ /* *
168
+ * @brief Set the vertex buffer data for compute shaders.
169
+ *
170
+ * @note The data must be available for two bgfx::frame calls, then it is
171
+ * safe to release the data. If you cannot guarantee this, you must provide
172
+ * a release function that will be called automatically when the data is no
173
+ * longer needed.
174
+ *
175
+ * @param[in] bufferData: the data to be copied in the vertex buffer.
176
+ * @param[in] bufferSize: the size of the bufferData.
177
+ * @param[in] attrib: the attribute to which the data refers.
178
+ * @param[in] numElements: the number of elements for each vertex.
179
+ * @param[in] type: the type of the elements.
180
+ * @param[in] normalize: if true, the data is normalized.
181
+ * @param[in] access: the access type for the buffer.
182
+ * @param[in] releaseFn: the release function to be called when the data is
183
+ * no longer needed.
184
+ */
95
185
void setForCompute (
96
186
const void * bufferData,
97
187
const uint bufferSize,
@@ -116,19 +206,34 @@ class VertexBuffer
116
206
flags);
117
207
}
118
208
209
+ /* *
210
+ * @brief Set the vertex buffer data.
211
+ *
212
+ * @param[in] layout: the vertex layout.
213
+ * @param[in] data: the memory containing the data.
214
+ * @param[in] compute: if true, the buffer is used for compute shaders.
215
+ * @param[in] flags: the flags for the buffer.
216
+ */
119
217
void set (
120
218
const bgfx::VertexLayout& layout,
121
- const bgfx::Memory* vertices ,
219
+ const bgfx::Memory* data ,
122
220
bool compute = false ,
123
221
uint64_t flags = BGFX_BUFFER_NONE)
124
222
{
125
223
if (bgfx::isValid (mVertexBufferHandle ))
126
224
bgfx::destroy (mVertexBufferHandle );
127
225
128
- mVertexBufferHandle = bgfx::createVertexBuffer (vertices , layout, flags);
226
+ mVertexBufferHandle = bgfx::createVertexBuffer (data , layout, flags);
129
227
mCompute = compute;
130
228
}
131
229
230
+ /* *
231
+ * @brief Bind the vertex buffer to the rendering pipeline.
232
+ *
233
+ * @param[in] stream: the stream (or stage, in case of compute) to which the
234
+ * vertex buffer is bound.
235
+ * @param[in] access: the access type for the buffer (only for compute).
236
+ */
132
237
void bind (uint stream, bgfx::Access::Enum access = bgfx::Access::Read) const
133
238
{
134
239
if (bgfx::isValid (mVertexBufferHandle )) {
0 commit comments