Visual Computing Library
Loading...
Searching...
No Matches
vertex_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_VERTEX_BUFFER_H
24#define VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
25
26#include "generic_buffer.h"
27
28namespace vcl {
29
42class VertexBuffer : public GenericBuffer<bgfx::VertexBufferHandle>
43{
45
46 bool mCompute = false;
47
48public:
54 VertexBuffer() = default;
55
62 {
63 using std::swap;
64 Base::swap(other);
65 swap(mCompute, other.mCompute);
66 }
67
68 friend void swap(VertexBuffer& a, VertexBuffer& b) { a.swap(b); }
69
76 bool isCompute() const { return mCompute; }
77
84 void setCompute(bool compute) { mCompute = compute; }
85
106 void create(
107 const void* bufferData,
109 bgfx::Attrib::Enum attrib,
112 bool normalize = false,
113 bgfx::ReleaseFn releaseFn = nullptr)
114 {
115 if (vertNum != 0) {
116 bgfx::VertexLayout layout;
117 layout.begin()
118 .add(
119 attrib,
121 attributeType(attribType),
122 normalize)
123 .end();
124
125 create(
126 bgfx::makeRef(
129 releaseFn),
130 layout);
131 }
132 else {
133 if (releaseFn)
134 releaseFn((void*) bufferData, nullptr);
135 destroy();
136 }
137 }
138
161 const void* bufferData,
162 const uint vertNum,
163 bgfx::Attrib::Enum attrib,
166 bool normalize = false,
167 bgfx::Access::Enum access = bgfx::Access::Read,
168 bgfx::ReleaseFn releaseFn = nullptr)
169 {
170 if (vertNum != 0) {
171 uint64_t flags = flagsForAccess(access);
172
173 bgfx::VertexLayout layout;
174 layout.begin()
175 .add(
176 attrib,
178 attributeType(attribType),
179 normalize)
180 .end();
181
182 create(
183 bgfx::makeRef(
186 releaseFn),
187 layout,
188 flags,
189 true);
190 }
191 else {
192 if (releaseFn)
193 releaseFn((void*) bufferData, nullptr);
194 destroy();
195 }
196 }
197
209 void create(
210 const bgfx::Memory* data,
211 const bgfx::VertexLayout& layout,
213 bool compute = false)
214 {
215 if (bgfx::isValid(mHandle))
216 bgfx::destroy(mHandle);
217
218 mHandle = bgfx::createVertexBuffer(data, layout, flags);
219 mCompute = compute;
220 }
221
231 void bind(uint stream, bgfx::Access::Enum access = bgfx::Access::Read) const
232 {
233 if (bgfx::isValid(mHandle)) {
234 if (!mCompute)
235 bgfx::setVertexBuffer(stream, mHandle);
236 else
237 bgfx::setBuffer(stream, mHandle, access);
238 }
239 }
240};
241
242} // namespace vcl
243
244#endif // VCL_BGFX_BUFFERS_VERTEX_BUFFER_H
The GenericBuffer manages the lifetime of a bgfx BufferHandle.
Definition generic_buffer.h:44
void destroy()
Definition generic_buffer.h:59
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The VertexBuffer manages the lifetime of a bgfx::VertexBufferHandle.
Definition vertex_buffer.h:43
VertexBuffer()=default
Empty constructor.
void swap(VertexBuffer &other)
Swap the content of this object with another VertexBuffer object.
Definition vertex_buffer.h:61
void create(const void *bufferData, uint vertNum, bgfx::Attrib::Enum attrib, uint attribNumPerVertex, PrimitiveType attribType, bool normalize=false, bgfx::ReleaseFn releaseFn=nullptr)
Creates the vertex buffer and sets the data for rendering.
Definition vertex_buffer.h:106
void create(const bgfx::Memory *data, const bgfx::VertexLayout &layout, uint64_t flags=BGFX_BUFFER_NONE, bool compute=false)
Creates the vertex buffer and sets the data.
Definition vertex_buffer.h:209
void createForCompute(const void *bufferData, const uint vertNum, bgfx::Attrib::Enum attrib, uint attribNumPerVertex, PrimitiveType attribType, bool normalize=false, bgfx::Access::Enum access=bgfx::Access::Read, bgfx::ReleaseFn releaseFn=nullptr)
Creates the vertex buffer and sets the data for compute shaders.
Definition vertex_buffer.h:160
void setCompute(bool compute)
Set if the VertexBuffer is used for compute shaders.
Definition vertex_buffer.h:84
bool isCompute() const
Check if the VertexBuffer is used for compute shaders.
Definition vertex_buffer.h:76
void bind(uint stream, bgfx::Access::Enum access=bgfx::Access::Read) const
Bind the vertex buffer to the rendering pipeline.
Definition vertex_buffer.h:231
PrimitiveType
A simple type that enumerates the main primitive types.
Definition base.h:58