Visual Computing Library
Loading...
Searching...
No Matches
index_buffer.h
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 "generic_buffer.h"
27
28namespace vcl {
29
42class IndexBuffer : public GenericBuffer<bgfx::IndexBufferHandle>
43{
45
46 bool mCompute = false;
47
48public:
54 IndexBuffer() = default;
55
62 {
63 using std::swap;
64 Base::swap(other);
65 swap(mCompute, other.mCompute);
66 }
67
68 friend void swap(IndexBuffer& a, IndexBuffer& b) { a.swap(b); }
69
76 bool isCompute() const { return mCompute; }
77
84 void setCompute(bool compute) { mCompute = compute; }
85
103 void create(
104 const void* bufferIndices,
105 const uint bufferSize,
106 bool is32Bit = true,
107 bgfx::ReleaseFn releaseFn = nullptr)
108 {
109 if (bufferSize != 0) {
111 uint size = is32Bit ? 4 : 2;
112 create(
113 bgfx::makeRef(bufferIndices, bufferSize * size, releaseFn),
114 flags);
115 }
116 else {
117 if (releaseFn)
118 releaseFn((void*) bufferIndices, nullptr);
119 destroy();
120 }
121 }
122
142 const void* bufferIndices,
143 const uint bufferSize,
144 PrimitiveType type,
145 bgfx::Access::Enum access = bgfx::Access::Read,
146 bgfx::ReleaseFn releaseFn = nullptr)
147 {
148 if (bufferSize != 0) {
149 uint64_t flags = flagsForType(type);
150 flags |= flagsForAccess(access);
151 create(
152 bgfx::makeRef(
153 bufferIndices, bufferSize * sizeOf(type), releaseFn),
154 flags,
155 true);
156 }
157 else {
158 if (releaseFn)
159 releaseFn((void*) bufferIndices, nullptr);
160 destroy();
161 }
162 }
163
174 void create(
175 const bgfx::Memory* indices,
177 bool compute = false)
178 {
179 mHandle = bgfx::createIndexBuffer(indices, flags);
180 mCompute = compute;
181 }
182
193 void bind(
195 bgfx::Access::Enum access = bgfx::Access::Read) const
196 {
197 if (bgfx::isValid(mHandle)) {
198 if (stage == UINT_NULL) {
199 bgfx::setIndexBuffer(mHandle);
200 }
201 else {
202 bgfx::setBuffer(stage, mHandle, access);
203 }
204 }
205 }
206};
207
208} // namespace vcl
209
210#endif // VCL_BGFX_BUFFERS_INDEX_BUFFER_H
The GenericBuffer manages the lifetime of a bgfx BufferHandle.
Definition generic_buffer.h:44
void destroy()
Definition generic_buffer.h:59
The IndexBuffer manages the lifetime of a bgfx::IndexBufferHandle.
Definition index_buffer.h:43
bool isCompute() const
Check if the IndexBuffer is used for compute shaders.
Definition index_buffer.h:76
void create(const void *bufferIndices, const uint bufferSize, bool is32Bit=true, bgfx::ReleaseFn releaseFn=nullptr)
Creates the index buffer and sets the data for rendering.
Definition index_buffer.h:103
IndexBuffer()=default
Empty constructor.
void bind(uint stage=UINT_NULL, bgfx::Access::Enum access=bgfx::Access::Read) const
Bind the index buffer to the rendering pipeline.
Definition index_buffer.h:193
void swap(IndexBuffer &other)
Swap the content of this object with another IndexBuffer object.
Definition index_buffer.h:61
void setCompute(bool compute)
Set if the IndexBuffer is used for compute shaders.
Definition index_buffer.h:84
void create(const bgfx::Memory *indices, uint64_t flags=BGFX_BUFFER_NONE, bool compute=false)
Creates the index buffer and sets the data.
Definition index_buffer.h:174
void createForCompute(const void *bufferIndices, const uint bufferSize, PrimitiveType type, bgfx::Access::Enum access=bgfx::Access::Read, bgfx::ReleaseFn releaseFn=nullptr)
Creates the index buffer and sets the data for compute shaders.
Definition index_buffer.h:141
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
PrimitiveType
A simple type that enumerates the main primitive types.
Definition base.h:58
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48