Skip to content

Commit 1939666

Browse files
committed
[render] documentation for VertexBuffer and IndexBuffer classes
1 parent aab5f24 commit 1939666

File tree

2 files changed

+231
-14
lines changed

2 files changed

+231
-14
lines changed

vclib/render/include/vclib/bgfx/buffers/index_buffer.h

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,117 @@
3131

3232
namespace vcl {
3333

34+
/**
35+
* @brief The IndexBuffer manages the lifetime of a bgfx::IndexBufferHandle.
36+
*
37+
* It provides an interface to set the index buffer data and bind it to the
38+
* rendering pipeline. The index buffer can be used for rendering or for
39+
* compute shaders.
40+
*
41+
* @note An IndexBuffer can be moved but not copied (a copy would require to
42+
* create a new bgfx::IndexBufferHandle, that can be done only having access
43+
* to the data). Any class that contains an IndexBuffer should implement the
44+
* copy constructor and the copy assignment operator.
45+
*/
3446
class IndexBuffer
3547
{
3648
bgfx::IndexBufferHandle mIndexBufferHandle = BGFX_INVALID_HANDLE;
49+
bool mCompute = false;
3750

3851
public:
52+
/**
53+
* @brief Empty constructor.
54+
*
55+
* It creates an invalid IndexBuffer object.
56+
*/
3957
IndexBuffer() = default;
4058

59+
// Copying an IndexBuffer is not allowed
4160
IndexBuffer(const IndexBuffer& other) = delete;
4261

62+
/**
63+
* @brief Move constructor.
64+
*
65+
* The other IndexBuffer is left in an invalid state.
66+
*
67+
* @param[in] other: the other IndexBuffer object.
68+
*/
4369
IndexBuffer(IndexBuffer&& other) noexcept
4470
{
45-
mIndexBufferHandle = other.mIndexBufferHandle;
46-
other.mIndexBufferHandle = BGFX_INVALID_HANDLE;
71+
swap(other);
4772
}
4873

74+
/**
75+
* @brief Destructor.
76+
*
77+
* It destroys the bgfx::IndexBufferHandle.
78+
*/
4979
~IndexBuffer()
5080
{
5181
if (bgfx::isValid(mIndexBufferHandle))
5282
bgfx::destroy(mIndexBufferHandle);
5383
}
5484

85+
// Copying a IndexBuffer is not allowed
5586
IndexBuffer& operator=(const IndexBuffer& other) = delete;
5687

88+
/**
89+
* @brief Move assignment operator.
90+
*
91+
* The other IndexBuffer is left in an invalid state.
92+
*
93+
* @param[in] other: the other IndexBuffer object.
94+
* @return a reference to this object.
95+
*/
5796
IndexBuffer& operator=(IndexBuffer&& other) noexcept
5897
{
59-
mIndexBufferHandle = other.mIndexBufferHandle;
60-
other.mIndexBufferHandle = BGFX_INVALID_HANDLE;
98+
swap(other);
6199
return *this;
62100
}
63101

102+
/**
103+
* @brief Swap the content of this object with another IndexBuffer object.
104+
*
105+
* @param[in] other: the other IndexBuffer object.
106+
*/
64107
void swap(IndexBuffer& other)
65108
{
66109
using std::swap;
67110
swap(mIndexBufferHandle, other.mIndexBufferHandle);
111+
swap(mCompute, other.mCompute);
68112
}
69113

70114
friend void swap(IndexBuffer& a, IndexBuffer& b) { a.swap(b); }
71115

116+
/**
117+
* @brief Check if the IndexBuffer is valid.
118+
*
119+
* @return true if the IndexBuffer is valid, false otherwise.
120+
*/
121+
bool isValid() const { return bgfx::isValid(mIndexBufferHandle); }
122+
123+
/**
124+
* @brief Check if the IndexBuffer is used for compute shaders.
125+
*
126+
* @return true if the IndexBuffer is used for compute shaders, false
127+
* otherwise.
128+
*/
129+
bool isCompute() const { return mCompute; }
130+
131+
/**
132+
* @brief Set the index buffer data.
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] bufferIndices: the data to be copied in the index buffer.
140+
* @param[in] bufferSize: the size of the bufferIndices.
141+
* @param[in] is32Bit: if true, the bufferIndices are 32-bit integers.
142+
* @param[in] releaseFn: the release function to be called when the data is
143+
* no longer needed.
144+
*/
72145
void set(
73146
const void* bufferIndices,
74147
const uint bufferSize,
@@ -77,9 +150,26 @@ class IndexBuffer
77150
{
78151
uint64_t flags = is32Bit ? BGFX_BUFFER_INDEX32 : BGFX_BUFFER_NONE;
79152
uint size = is32Bit ? 4 : 2;
80-
set(bgfx::makeRef(bufferIndices, bufferSize * size, releaseFn), flags);
153+
set(bgfx::makeRef(bufferIndices, bufferSize * size, releaseFn),
154+
false,
155+
flags);
81156
}
82157

158+
/**
159+
* @brief Set the index buffer data for compute shaders.
160+
*
161+
* @note The data must be available for two bgfx::frame calls, then it is
162+
* safe to release the data. If you cannot guarantee this, you must provide
163+
* a release function that will be called automatically when the data is no
164+
* longer needed.
165+
*
166+
* @param[in] bufferIndices: the data to be copied in the index buffer.
167+
* @param[in] bufferSize: the size of the bufferIndices.
168+
* @param[in] type: the type of the elements.
169+
* @param[in] access: the access type for the buffer.
170+
* @param[in] releaseFn: the release function to be called when the data is
171+
* no longer needed.
172+
*/
83173
void setForCompute(
84174
const void* bufferIndices,
85175
const uint bufferSize,
@@ -90,14 +180,36 @@ class IndexBuffer
90180
uint64_t flags = flagsForType(type);
91181
flags |= flagsForAccess(access);
92182
set(bgfx::makeRef(bufferIndices, bufferSize * sizeOf(type), releaseFn),
183+
true,
93184
flags);
94185
}
95186

96-
void set(const bgfx::Memory* indices, uint64_t flags = BGFX_BUFFER_NONE)
187+
/**
188+
* @brief Set the index buffer data.
189+
*
190+
* @param[in] indices: the memory containing the data.
191+
* @param[in] compute: if true, the buffer is used for compute shaders.
192+
* @param[in] flags: the flags for the buffer.
193+
*/
194+
void set(
195+
const bgfx::Memory* indices,
196+
bool compute = false,
197+
uint64_t flags = BGFX_BUFFER_NONE)
97198
{
98199
mIndexBufferHandle = bgfx::createIndexBuffer(indices, flags);
200+
mCompute = compute;
99201
}
100202

203+
/**
204+
* @brief Bind the index buffer to the rendering pipeline.
205+
*
206+
* If the stage is not specified, the index buffer is bound to the rendering
207+
* pipeline. Otherwise, it is bound to the compute shader with the specified
208+
* stage and the given access type (Read, Write, ReadWrite).
209+
*
210+
* @param[in] stage: the stage to which the buffer is bound.
211+
* @param[in] access: the access type for the buffer.
212+
*/
101213
void bind(
102214
uint stage = UINT_NULL,
103215
bgfx::Access::Enum access = bgfx::Access::Read) const

vclib/render/include/vclib/bgfx/buffers/vertex_buffer.h

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,79 @@
3131

3232
namespace vcl {
3333

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+
*/
3446
class VertexBuffer
3547
{
3648
bgfx::VertexBufferHandle mVertexBufferHandle = BGFX_INVALID_HANDLE;
3749
bool mCompute = false;
3850

3951
public:
52+
/**
53+
* @brief Empty constructor.
54+
*
55+
* It creates an invalid VertexBuffer object.
56+
*/
4057
VertexBuffer() = default;
4158

59+
// Copying a VertexBuffer is not allowed
4260
VertexBuffer(const VertexBuffer& other) = delete;
4361

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+
*/
4469
VertexBuffer(VertexBuffer&& other) noexcept
4570
{
46-
mVertexBufferHandle = other.mVertexBufferHandle;
47-
mCompute = other.mCompute;
48-
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
71+
swap(other);
4972
}
5073

74+
/**
75+
* @brief Destructor.
76+
*
77+
* If the VertexBuffer is valid, the bgfx::VertexBufferHandle is destroyed.
78+
*/
5179
~VertexBuffer()
5280
{
5381
if (bgfx::isValid(mVertexBufferHandle))
5482
bgfx::destroy(mVertexBufferHandle);
5583
}
5684

85+
// Copying a VertexBuffer is not allowed
5786
VertexBuffer& operator=(const VertexBuffer& other) = delete;
5887

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+
*/
5996
VertexBuffer& operator=(VertexBuffer&& other) noexcept
6097
{
61-
mVertexBufferHandle = other.mVertexBufferHandle;
62-
mCompute = other.mCompute;
63-
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
98+
swap(other);
6499
return *this;
65100
}
66101

102+
/**
103+
* @brief Swap the content of this object with another VertexBuffer object.
104+
*
105+
* @param[in] other: the other VertexBuffer object.
106+
*/
67107
void swap(VertexBuffer& other)
68108
{
69109
using std::swap;
@@ -73,6 +113,38 @@ class VertexBuffer
73113

74114
friend void swap(VertexBuffer& a, VertexBuffer& b) { a.swap(b); }
75115

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+
*/
76148
void set(
77149
const void* bufferData,
78150
const uint bufferSize,
@@ -92,6 +164,24 @@ class VertexBuffer
92164
releaseFn));
93165
}
94166

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+
*/
95185
void setForCompute(
96186
const void* bufferData,
97187
const uint bufferSize,
@@ -116,19 +206,34 @@ class VertexBuffer
116206
flags);
117207
}
118208

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+
*/
119217
void set(
120218
const bgfx::VertexLayout& layout,
121-
const bgfx::Memory* vertices,
219+
const bgfx::Memory* data,
122220
bool compute = false,
123221
uint64_t flags = BGFX_BUFFER_NONE)
124222
{
125223
if (bgfx::isValid(mVertexBufferHandle))
126224
bgfx::destroy(mVertexBufferHandle);
127225

128-
mVertexBufferHandle = bgfx::createVertexBuffer(vertices, layout, flags);
226+
mVertexBufferHandle = bgfx::createVertexBuffer(data, layout, flags);
129227
mCompute = compute;
130228
}
131229

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+
*/
132237
void bind(uint stream, bgfx::Access::Enum access = bgfx::Access::Read) const
133238
{
134239
if (bgfx::isValid(mVertexBufferHandle)) {

0 commit comments

Comments
 (0)