|
| 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_INDEX_BUFFER_H |
| 24 | +#define VCL_BGFX_BUFFERS_INDEX_BUFFER_H |
| 25 | + |
| 26 | +#include <vclib/types.h> |
| 27 | + |
| 28 | +#include <bgfx/bgfx.h> |
| 29 | + |
| 30 | +#include <utility> |
| 31 | + |
| 32 | +namespace vcl { |
| 33 | + |
| 34 | +class IndexBuffer |
| 35 | +{ |
| 36 | + bgfx::IndexBufferHandle mIndexBufferHandle = BGFX_INVALID_HANDLE; |
| 37 | + |
| 38 | +public: |
| 39 | + IndexBuffer() = default; |
| 40 | + |
| 41 | + IndexBuffer(const IndexBuffer& other) = delete; |
| 42 | + |
| 43 | + IndexBuffer(IndexBuffer&& other) noexcept |
| 44 | + { |
| 45 | + mIndexBufferHandle = other.mIndexBufferHandle; |
| 46 | + other.mIndexBufferHandle = BGFX_INVALID_HANDLE; |
| 47 | + } |
| 48 | + |
| 49 | + ~IndexBuffer() |
| 50 | + { |
| 51 | + if (bgfx::isValid(mIndexBufferHandle)) |
| 52 | + bgfx::destroy(mIndexBufferHandle); |
| 53 | + } |
| 54 | + |
| 55 | + IndexBuffer& operator=(const IndexBuffer& other) = delete; |
| 56 | + |
| 57 | + IndexBuffer& operator=(IndexBuffer&& other) noexcept |
| 58 | + { |
| 59 | + mIndexBufferHandle = other.mIndexBufferHandle; |
| 60 | + other.mIndexBufferHandle = BGFX_INVALID_HANDLE; |
| 61 | + return *this; |
| 62 | + } |
| 63 | + |
| 64 | + void swap(IndexBuffer& other) |
| 65 | + { |
| 66 | + using std::swap; |
| 67 | + swap(mIndexBufferHandle, other.mIndexBufferHandle); |
| 68 | + } |
| 69 | + |
| 70 | + friend void swap(IndexBuffer& a, IndexBuffer& b) { a.swap(b); } |
| 71 | + |
| 72 | + void set( |
| 73 | + const void* bufferIndices, |
| 74 | + const uint bufferSize, |
| 75 | + PrimitiveType type, |
| 76 | + uint64_t flags = BGFX_BUFFER_NONE) |
| 77 | + { |
| 78 | + flags |= flagsForType(type); |
| 79 | + set(bgfx::makeRef(bufferIndices, bufferSize * sizeOf(type)), flags); |
| 80 | + } |
| 81 | + |
| 82 | + void set(const bgfx::Memory* indices, uint64_t flags = BGFX_BUFFER_NONE) |
| 83 | + { |
| 84 | + mIndexBufferHandle = bgfx::createIndexBuffer(indices, flags); |
| 85 | + } |
| 86 | + |
| 87 | + void bind() const |
| 88 | + { |
| 89 | + bgfx::setIndexBuffer(mIndexBufferHandle); |
| 90 | + } |
| 91 | + |
| 92 | + void bindForCompute( |
| 93 | + uint8_t stage, |
| 94 | + bgfx::Access::Enum access = bgfx::Access::Read) |
| 95 | + { |
| 96 | + bgfx::setBuffer(stage, mIndexBufferHandle, access); |
| 97 | + } |
| 98 | + |
| 99 | +private: |
| 100 | + static uint64_t flagsForType(PrimitiveType type) |
| 101 | + { |
| 102 | + switch (type) { |
| 103 | + case PrimitiveType::INT: |
| 104 | + case PrimitiveType::UINT: return BGFX_BUFFER_INDEX32; |
| 105 | + case PrimitiveType::FLOAT: |
| 106 | + return BGFX_BUFFER_COMPUTE_FORMAT_32X1 | |
| 107 | + BGFX_BUFFER_COMPUTE_TYPE_FLOAT; |
| 108 | + case PrimitiveType::DOUBLE: assert(0); // not supported |
| 109 | + default: return BGFX_BUFFER_NONE; |
| 110 | + } |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace vcl |
| 115 | + |
| 116 | +#endif // VCL_BGFX_BUFFERS_INDEX_BUFFER_H |
0 commit comments