|
| 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 |
0 commit comments