Visual Computing Library
Loading...
Searching...
No Matches
generic_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_GENERIC_BUFFER_H
24#define VCL_BGFX_BUFFERS_GENERIC_BUFFER_H
25
26#include <vclib/types.h>
27
28#include <bgfx/bgfx.h>
29
30#include <utility>
31
32namespace vcl {
33
42template<typename BufferHandleType>
44{
45protected:
47
48public:
54 bool isValid() const { return bgfx::isValid(mHandle); }
55
59 void destroy()
60 {
61 if (bgfx::isValid(mHandle)) {
62 bgfx::destroy(mHandle);
63 mHandle = BGFX_INVALID_HANDLE;
64 }
65 }
66
67protected:
73 GenericBuffer() = default;
74
75 // Copying a GenericBuffer is not allowed
76 GenericBuffer(const GenericBuffer& other) = delete;
77
86
93
94 // Copying a GenericBuffer is not allowed
95 GenericBuffer& operator=(const GenericBuffer& other) = delete;
96
106 {
107 swap(other);
108 return *this;
109 }
110
117 {
118 using std::swap;
119 swap(mHandle, other.mHandle);
120 }
121
122 friend void swap(GenericBuffer& a, GenericBuffer& b) { a.swap(b); }
123
124protected:
125 static uint64_t flagsForAccess(bgfx::Access::Enum access)
126 {
127 switch (access) {
128 case bgfx::Access::Read: return BGFX_BUFFER_COMPUTE_READ;
129 case bgfx::Access::Write: return BGFX_BUFFER_COMPUTE_WRITE;
130 case bgfx::Access::ReadWrite: return BGFX_BUFFER_COMPUTE_READ_WRITE;
131 default: return BGFX_BUFFER_NONE;
132 }
133 }
134
135 static bgfx::AttribType::Enum attributeType(PrimitiveType type)
136 {
137 switch (type) {
138 case PrimitiveType::CHAR:
139 case PrimitiveType::UCHAR: return bgfx::AttribType::Uint8;
140 case PrimitiveType::SHORT:
141 case PrimitiveType::USHORT: return bgfx::AttribType::Int16;
142 case PrimitiveType::FLOAT: return bgfx::AttribType::Float;
143 default:
144 assert(0); // not supported
145 return bgfx::AttribType::Count;
146 }
147 }
148
149 static uint64_t flagsForType(PrimitiveType type)
150 {
151 switch (type) {
152 case PrimitiveType::INT:
153 case PrimitiveType::UINT: return BGFX_BUFFER_INDEX32;
154 case PrimitiveType::FLOAT:
155 return BGFX_BUFFER_COMPUTE_FORMAT_32X1 |
156 BGFX_BUFFER_COMPUTE_TYPE_FLOAT;
157 case PrimitiveType::DOUBLE: assert(0); // not supported
158 default: return BGFX_BUFFER_NONE;
159 }
160 }
161};
162
163} // namespace vcl
164
165#endif // VCL_BGFX_BUFFERS_GENERIC_BUFFER_H
The GenericBuffer manages the lifetime of a bgfx BufferHandle.
Definition generic_buffer.h:44
void destroy()
Destroy the Buffer.
Definition generic_buffer.h:59
GenericBuffer(GenericBuffer &&other) noexcept
Move constructor.
Definition generic_buffer.h:85
GenericBuffer()=default
Empty constructor.
GenericBuffer & operator=(GenericBuffer &&other) noexcept
Move assignment operator.
Definition generic_buffer.h:105
void swap(GenericBuffer &other)
Swap the content of this object with another Buffer object.
Definition generic_buffer.h:116
~GenericBuffer()
Destructor.
Definition generic_buffer.h:92
bool isValid() const
Check if the Buffer is valid.
Definition generic_buffer.h:54
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