Skip to content

Commit 231e775

Browse files
committed
[render] VertexBuffer class
1 parent 85ba3b8 commit 231e775

File tree

3 files changed

+158
-17
lines changed

3 files changed

+158
-17
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*****************************************************************************
2+
* VCLib *
3+
* Visual Computing Library *
4+
* *
5+
* Copyright(C) 2021-2025 *
6+
* Visual Computing Lab *
7+
* ISTI - Italian National Research Council *
8+
* *
9+
* All rights reserved. *
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the Mozilla Public License Version 2.0 as published *
13+
* by the Mozilla Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
* This program is distributed in the hope that it will be useful, *
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19+
* Mozilla Public License Version 2.0 *
20+
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21+
****************************************************************************/
22+
23+
#ifndef VCL_BGFX_BUFFERS_H
24+
#define VCL_BGFX_BUFFERS_H
25+
26+
#include "buffers/vertex_buffer.h"
27+
28+
#endif // VCL_BGFX_BUFFERS_H
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*****************************************************************************
2+
* VCLib *
3+
* Visual Computing Library *
4+
* *
5+
* Copyright(C) 2021-2025 *
6+
* Visual Computing Lab *
7+
* ISTI - Italian National Research Council *
8+
* *
9+
* All rights reserved. *
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the Mozilla Public License Version 2.0 as published *
13+
* by the Mozilla Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
* This program is distributed in the hope that it will be useful, *
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19+
* Mozilla Public License Version 2.0 *
20+
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21+
****************************************************************************/
22+
23+
#ifndef VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
24+
#define VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
25+
26+
#include <bgfx/bgfx.h>
27+
#include <utility>
28+
29+
namespace vcl {
30+
31+
class VertexBuffer
32+
{
33+
bgfx::VertexBufferHandle mVertexBufferHandle = BGFX_INVALID_HANDLE;
34+
35+
public:
36+
VertexBuffer() = default;
37+
38+
VertexBuffer(const VertexBuffer& other) = delete;
39+
40+
VertexBuffer(VertexBuffer&& other) noexcept
41+
{
42+
mVertexBufferHandle = other.mVertexBufferHandle;
43+
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
44+
}
45+
46+
~VertexBuffer()
47+
{
48+
if (bgfx::isValid(mVertexBufferHandle))
49+
bgfx::destroy(mVertexBufferHandle);
50+
}
51+
52+
VertexBuffer& operator=(const VertexBuffer& other) = delete;
53+
54+
VertexBuffer& operator=(VertexBuffer&& other) noexcept
55+
{
56+
mVertexBufferHandle = other.mVertexBufferHandle;
57+
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
58+
return *this;
59+
}
60+
61+
void swap(VertexBuffer& other)
62+
{
63+
using std::swap;
64+
swap(mVertexBufferHandle, other.mVertexBufferHandle);
65+
}
66+
67+
friend void swap(VertexBuffer& a, VertexBuffer& b) { a.swap(b); }
68+
69+
void set(
70+
const void* bufferData,
71+
const uint bufferSize,
72+
bgfx::Attrib::Enum attrib,
73+
uint numElements,
74+
bgfx::AttribType::Enum attribType,
75+
bgfx::ReleaseFn releaseFn = nullptr,
76+
uint64_t flags = BGFX_BUFFER_NONE)
77+
{
78+
bgfx::VertexLayout layout;
79+
layout.begin().add(attrib, numElements, attribType).end();
80+
81+
set(layout,
82+
bgfx::makeRef(
83+
bufferData, bufferSize * attribTypeSize(attribType), releaseFn),
84+
flags);
85+
}
86+
87+
void set(
88+
const bgfx::VertexLayout& layout,
89+
const bgfx::Memory* vertices,
90+
uint64_t flags = BGFX_BUFFER_NONE)
91+
{
92+
if (bgfx::isValid(mVertexBufferHandle))
93+
bgfx::destroy(mVertexBufferHandle);
94+
95+
mVertexBufferHandle = bgfx::createVertexBuffer(vertices, layout, flags);
96+
}
97+
98+
void bind(uint stream) const
99+
{
100+
if (bgfx::isValid(mVertexBufferHandle))
101+
bgfx::setVertexBuffer(stream, mVertexBufferHandle);
102+
}
103+
104+
static int attribTypeSize(bgfx::AttribType::Enum type)
105+
{
106+
switch(type)
107+
{
108+
case bgfx::AttribType::Uint8: return 1;
109+
case bgfx::AttribType::Uint10: return 2;
110+
case bgfx::AttribType::Int16: return 2;
111+
case bgfx::AttribType::Half: return 2;
112+
case bgfx::AttribType::Float: return 4;
113+
default: return 0;
114+
}
115+
}
116+
};
117+
118+
} // namespace vcl
119+
120+
#endif // VCL_BGFX_BUFFERS_VERTEX_BUFFER_H

vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "mesh_render_buffers_macros.h"
2727

28+
#include <vclib/bgfx/buffers.h>
2829
#include <vclib/render/drawable/mesh/mesh_render_data.h>
2930

3031
#include <bgfx/bgfx.h>
@@ -36,7 +37,7 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
3637
{
3738
using Base = vcl::MeshRenderData<MeshType>;
3839

39-
bgfx::VertexBufferHandle mVertexCoordBH = BGFX_INVALID_HANDLE;
40+
VertexBuffer mVertexBuffer;
4041
bgfx::VertexBufferHandle mVertexNormalBH = BGFX_INVALID_HANDLE;
4142
bgfx::VertexBufferHandle mVertexColorBH = BGFX_INVALID_HANDLE;
4243
bgfx::VertexBufferHandle mVertexUVBH = BGFX_INVALID_HANDLE;
@@ -85,7 +86,7 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
8586
{
8687
using std::swap;
8788
swap((Base&) *this, (Base&) other);
88-
swap(mVertexCoordBH, other.mVertexCoordBH);
89+
swap(mVertexBuffer, other.mVertexBuffer);
8990
swap(mVertexNormalBH, other.mVertexNormalBH);
9091
swap(mVertexColorBH, other.mVertexColorBH);
9192
swap(mVertexUVBH, other.mVertexUVBH);
@@ -114,7 +115,7 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
114115
{
115116
// bgfx allows a maximum number of 4 vertex streams...
116117

117-
bgfx::setVertexBuffer(0, mVertexCoordBH);
118+
mVertexBuffer.bind(0);
118119

119120
if (bgfx::isValid(mVertexNormalBH)) { // vertex normals
120121
bgfx::setVertexBuffer(1, mVertexNormalBH);
@@ -196,17 +197,12 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
196197
private:
197198
void createBGFXBuffers()
198199
{
199-
// vertex buffer (positions)
200-
bgfx::VertexLayout layout;
201-
layout.begin()
202-
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
203-
.end();
204-
205-
mVertexCoordBH = bgfx::createVertexBuffer(
206-
bgfx::makeRef(
207-
Base::vertexBufferData(),
208-
Base::vertexBufferSize() * sizeof(float)),
209-
layout);
200+
mVertexBuffer.set(
201+
Base::vertexBufferData(),
202+
Base::vertexBufferSize(),
203+
bgfx::Attrib::Position,
204+
3,
205+
bgfx::AttribType::Float);
210206

211207
// vertex buffer (normals)
212208
if (Base::vertexNormalBufferData()) {
@@ -370,9 +366,6 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
370366

371367
void destroyBGFXBuffers()
372368
{
373-
if (bgfx::isValid(mVertexCoordBH))
374-
bgfx::destroy(mVertexCoordBH);
375-
376369
if (bgfx::isValid(mVertexNormalBH))
377370
bgfx::destroy(mVertexNormalBH);
378371

0 commit comments

Comments
 (0)