Visual Computing Library
Loading...
Searching...
No Matches
dynamic_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_DYNAMIC_VERTEX_BUFFER_H
24#define VCL_BGFX_BUFFERS_DYNAMIC_VERTEX_BUFFER_H
25
26#include "generic_buffer.h"
27
28namespace vcl {
29
47 public GenericBuffer<bgfx::DynamicVertexBufferHandle>
48{
50
51 bool mCompute = false;
52
53public:
60
68 {
69 using std::swap;
70 Base::swap(other);
71 swap(mCompute, other.mCompute);
72 }
73
74 friend void swap(DynamicVertexBuffer& a, DynamicVertexBuffer& b)
75 {
76 a.swap(b);
77 }
78
85 bool isCompute() const { return mCompute; }
86
101 void create(
103 bgfx::Attrib::Enum attrib,
106 bool normalize = false,
107 bool allowResize = true)
108 {
109 uint64_t flags =
111
112 bgfx::VertexLayout layout;
113 layout.begin()
114 .add(
115 attrib,
117 attributeType(attribType),
118 normalize)
119 .end();
120
121 create(vertNum, layout, flags);
122 }
123
141 void create(
142 const void* bufferData,
144 bgfx::Attrib::Enum attrib,
147 bool normalize = false,
148 bgfx::ReleaseFn releaseFn = nullptr,
149 bool allowResize = true)
150 {
151 uint64_t flags =
153
154 bgfx::VertexLayout layout;
155 layout.begin()
156 .add(
157 attrib,
159 attributeType(attribType),
160 normalize)
161 .end();
162
163 create(vertNum, layout, flags);
164 update(
166 }
167
177 void create(
179 const bgfx::VertexLayout& layout,
181 bool compute = false)
182 {
183 destroy();
184
185 if (vertNum != 0)
186 mHandle = bgfx::createDynamicVertexBuffer(vertNum, layout, flags);
187 mCompute = compute;
188 }
189
206 void update(
207 const void* bufferData,
211 uint startIndex = 0,
212 bgfx::ReleaseFn releaseFn = nullptr)
213 {
214 if (vertNum != 0) {
215 const bgfx::Memory* data = bgfx::makeRef(
218 releaseFn);
219
220 update(startIndex, data);
221 }
222 else {
223 if (releaseFn)
224 releaseFn((void*) bufferData, nullptr);
225 }
226 }
227
228 void update(uint startIndex, const bgfx::Memory* data)
229 {
230 if (bgfx::isValid(mHandle)) {
231 bgfx::update(mHandle, startIndex, data);
232 }
233 }
234
242 void bind(uint stream, bgfx::Access::Enum access = bgfx::Access::Read) const
243 {
244 if (bgfx::isValid(mHandle)) {
245 if (!mCompute)
246 bgfx::setVertexBuffer(stream, mHandle);
247 else
248 bgfx::setBuffer(stream, mHandle, access);
249 }
250 }
251};
252
253} // namespace vcl
254
255#endif // VCL_BGFX_BUFFERS_DYNAMIC_VERTEX_BUFFER_H
The DynamicVertexBuffer manages the lifetime of a bgfx::DynamicVertexBufferHandle.
Definition dynamic_vertex_buffer.h:48
void create(uint vertNum, const bgfx::VertexLayout &layout, uint64_t flags=BGFX_BUFFER_NONE, bool compute=false)
Creates the dynamic vertex buffer data for rendering, with the given layout and without any data.
Definition dynamic_vertex_buffer.h:177
void create(uint vertNum, bgfx::Attrib::Enum attrib, uint attribNumPerVertex, PrimitiveType attribType, bool normalize=false, bool allowResize=true)
Creates the dynamic vertex buffer data for rendering, with the layout given by the vertex attributes ...
Definition dynamic_vertex_buffer.h:101
DynamicVertexBuffer()=default
Empty constructor.
void swap(DynamicVertexBuffer &other)
Swap the content of this object with another DynamicVertexBuffer object.
Definition dynamic_vertex_buffer.h:67
bool isCompute() const
Check if the VertexBuffer is used for compute shaders.
Definition dynamic_vertex_buffer.h:85
void update(const void *bufferData, uint vertNum, uint attribNumPerVertex, PrimitiveType attribType, uint startIndex=0, bgfx::ReleaseFn releaseFn=nullptr)
Updates the dynamic vertex buffer with the given data.
Definition dynamic_vertex_buffer.h:206
void create(const void *bufferData, uint vertNum, bgfx::Attrib::Enum attrib, uint attribNumPerVertex, PrimitiveType attribType, bool normalize=false, bgfx::ReleaseFn releaseFn=nullptr, bool allowResize=true)
Creates the dynamic vertex buffer data for rendering, with the layout given by the vertex attributes ...
Definition dynamic_vertex_buffer.h:141
void bind(uint stream, bgfx::Access::Enum access=bgfx::Access::Read) const
Bind the dynamic vertex buffer to the rendering pipeline.
Definition dynamic_vertex_buffer.h:242
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
PrimitiveType
A simple type that enumerates the main primitive types.
Definition base.h:58