Skip to content

Commit

Permalink
[render] VertexBuffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 23, 2025
1 parent 85ba3b8 commit 026f928
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 17 deletions.
28 changes: 28 additions & 0 deletions vclib/render/include/vclib/bgfx/buffers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*****************************************************************************
* VCLib *
* Visual Computing Library *
* *
* Copyright(C) 2021-2025 *
* Visual Computing Lab *
* ISTI - Italian National Research Council *
* *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the Mozilla Public License Version 2.0 as published *
* by the Mozilla Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Mozilla Public License Version 2.0 *
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
****************************************************************************/

#ifndef VCL_BGFX_BUFFERS_H
#define VCL_BGFX_BUFFERS_H

#include "buffers/vertex_buffer.h"

#endif // VCL_BGFX_BUFFERS_H
123 changes: 123 additions & 0 deletions vclib/render/include/vclib/bgfx/buffers/vertex_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*****************************************************************************
* VCLib *
* Visual Computing Library *
* *
* Copyright(C) 2021-2025 *
* Visual Computing Lab *
* ISTI - Italian National Research Council *
* *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the Mozilla Public License Version 2.0 as published *
* by the Mozilla Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Mozilla Public License Version 2.0 *
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
****************************************************************************/

#ifndef VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
#define VCL_BGFX_BUFFERS_VERTEX_BUFFER_H

#include <vclib/types.h>

#include <bgfx/bgfx.h>

#include <utility>

namespace vcl {

class VertexBuffer
{
bgfx::VertexBufferHandle mVertexBufferHandle = BGFX_INVALID_HANDLE;

public:
VertexBuffer() = default;

VertexBuffer(const VertexBuffer& other) = delete;

VertexBuffer(VertexBuffer&& other) noexcept
{
mVertexBufferHandle = other.mVertexBufferHandle;
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
}

~VertexBuffer()
{
if (bgfx::isValid(mVertexBufferHandle))
bgfx::destroy(mVertexBufferHandle);
}

VertexBuffer& operator=(const VertexBuffer& other) = delete;

VertexBuffer& operator=(VertexBuffer&& other) noexcept
{
mVertexBufferHandle = other.mVertexBufferHandle;
other.mVertexBufferHandle = BGFX_INVALID_HANDLE;
return *this;
}

void swap(VertexBuffer& other)
{
using std::swap;
swap(mVertexBufferHandle, other.mVertexBufferHandle);
}

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

void set(
const void* bufferData,
const uint bufferSize,
bgfx::Attrib::Enum attrib,
uint numElements,
bgfx::AttribType::Enum attribType,
bgfx::ReleaseFn releaseFn = nullptr,
uint64_t flags = BGFX_BUFFER_NONE)
{
bgfx::VertexLayout layout;
layout.begin().add(attrib, numElements, attribType).end();

set(layout,
bgfx::makeRef(
bufferData, bufferSize * attribTypeSize(attribType), releaseFn),
flags);
}

void set(
const bgfx::VertexLayout& layout,
const bgfx::Memory* vertices,
uint64_t flags = BGFX_BUFFER_NONE)
{
if (bgfx::isValid(mVertexBufferHandle))
bgfx::destroy(mVertexBufferHandle);

mVertexBufferHandle = bgfx::createVertexBuffer(vertices, layout, flags);
}

void bind(uint stream) const
{
if (bgfx::isValid(mVertexBufferHandle))
bgfx::setVertexBuffer(stream, mVertexBufferHandle);
}

static int attribTypeSize(bgfx::AttribType::Enum type)
{
switch(type)
{
case bgfx::AttribType::Uint8: return 1;
case bgfx::AttribType::Uint10: return 2;
case bgfx::AttribType::Int16: return 2;
case bgfx::AttribType::Half: return 2;
case bgfx::AttribType::Float: return 4;
default: return 0;
}
}
};

} // namespace vcl

#endif // VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
27 changes: 10 additions & 17 deletions vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "mesh_render_buffers_macros.h"

#include <vclib/bgfx/buffers.h>
#include <vclib/render/drawable/mesh/mesh_render_data.h>

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

bgfx::VertexBufferHandle mVertexCoordBH = BGFX_INVALID_HANDLE;
VertexBuffer mVertexBuffer;
bgfx::VertexBufferHandle mVertexNormalBH = BGFX_INVALID_HANDLE;
bgfx::VertexBufferHandle mVertexColorBH = BGFX_INVALID_HANDLE;
bgfx::VertexBufferHandle mVertexUVBH = BGFX_INVALID_HANDLE;
Expand Down Expand Up @@ -85,7 +86,7 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
{
using std::swap;
swap((Base&) *this, (Base&) other);
swap(mVertexCoordBH, other.mVertexCoordBH);
swap(mVertexBuffer, other.mVertexBuffer);
swap(mVertexNormalBH, other.mVertexNormalBH);
swap(mVertexColorBH, other.mVertexColorBH);
swap(mVertexUVBH, other.mVertexUVBH);
Expand Down Expand Up @@ -114,7 +115,7 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
{
// bgfx allows a maximum number of 4 vertex streams...

bgfx::setVertexBuffer(0, mVertexCoordBH);
mVertexBuffer.bind(0);

if (bgfx::isValid(mVertexNormalBH)) { // vertex normals
bgfx::setVertexBuffer(1, mVertexNormalBH);
Expand Down Expand Up @@ -196,17 +197,12 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>
private:
void createBGFXBuffers()
{
// vertex buffer (positions)
bgfx::VertexLayout layout;
layout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.end();

mVertexCoordBH = bgfx::createVertexBuffer(
bgfx::makeRef(
Base::vertexBufferData(),
Base::vertexBufferSize() * sizeof(float)),
layout);
mVertexBuffer.set(
Base::vertexBufferData(),
Base::vertexBufferSize(),
bgfx::Attrib::Position,
3,
bgfx::AttribType::Float);

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

void destroyBGFXBuffers()
{
if (bgfx::isValid(mVertexCoordBH))
bgfx::destroy(mVertexCoordBH);

if (bgfx::isValid(mVertexNormalBH))
bgfx::destroy(mVertexNormalBH);

Expand Down

0 comments on commit 026f928

Please sign in to comment.