VTK  9.6.20260222
vtkOpenGLBufferObject.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#ifndef vtkOpenGLBufferObject_h
4#define vtkOpenGLBufferObject_h
5
6#include "vtkObject.h"
7#include "vtkRenderingOpenGL2Module.h" // for export macro
8#include <cstddef> // for ptrdiff_t
9#include <string> // used for std::string
10#include <vector> // used for method args
11
12VTK_ABI_NAMESPACE_BEGIN
13class vtkCellArray;
14class vtkDataArray;
15class vtkPoints;
16
23
24class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLBufferObject : public vtkObject
25{
26public:
29 void PrintSelf(ostream& os, vtkIndent indent) override;
30
49
51
53 void SetType(ObjectType value);
54
57
59 void SetUsage(ObjectUsage value);
60
62 int GetHandle() const;
63
65 bool IsReady() const { return this->Dirty == false; }
66
68 void FlagBufferAsDirty() { this->Dirty = true; }
69
72
82 template <class T>
83 bool Upload(const T& array, ObjectType type);
84 template <class T>
85 bool UploadRange(const T& array, ptrdiff_t offset, ObjectType type);
86 // non vector version
87 template <class T>
88 bool Upload(const T* array, size_t numElements, ObjectType type);
89 template <class T>
90 bool UploadRange(const T* array, ptrdiff_t offset, size_t numElements, ObjectType type);
94 bool Allocate(size_t size, ObjectType type, ObjectUsage usage);
95
99 size_t GetSize();
100
104 template <class T>
105 bool Download(T* array, size_t numElements);
106 template <class T>
107 bool DownloadRange(T* array, ptrdiff_t offset, size_t numElements);
108
114 bool Bind();
115
119 bool BindShaderStorage(int index);
120
124 bool Release();
125
126 // Description:
127 // Release any graphics resources that are being consumed by this class.
129
133 std::string GetError() const { return Error; }
134
135protected:
138 bool Dirty;
139 std::string Error;
140
141 bool UploadInternal(const void* buffer, size_t size, ObjectType objectType);
143 const void* buffer, ptrdiff_t offset, ptrdiff_t size, ObjectType objectType);
144
145private:
146 bool DownloadRangeInternal(void* buffer, ptrdiff_t offset, size_t size);
147
149 void operator=(const vtkOpenGLBufferObject&) = delete;
150 struct Private;
151 Private* Internal;
152};
153
154template <class T>
156 const T& array, vtkOpenGLBufferObject::ObjectType objectType)
157{
158 if (array.empty())
159 {
160 this->Error = "Refusing to upload empty array.";
161 return false;
162 }
163
164 return this->UploadInternal(
165 array.data(), array.size() * sizeof(typename T::value_type), objectType);
166}
167
168template <class T>
170 const T* array, size_t numElements, vtkOpenGLBufferObject::ObjectType objectType)
171{
172 if (!array)
173 {
174 this->Error = "Refusing to upload empty array.";
175 return false;
176 }
177 return this->UploadInternal(array, numElements * sizeof(T), objectType);
178}
179
180template <class T>
182 const T& array, ptrdiff_t offset, vtkOpenGLBufferObject::ObjectType objectType)
183{
184 if (array.empty())
185 {
186 this->Error = "Refusing to upload empty array.";
187 return false;
188 }
189
190 return this->UploadRangeInternal(
191 array.data(), offset, array.size() * sizeof(typename T::value_type), objectType);
192}
193template <class T>
194inline bool vtkOpenGLBufferObject::UploadRange(const T* array, ptrdiff_t offset, size_t numElements,
196{
197 if (!array)
198 {
199 this->Error = "Refusing to upload empty array.";
200 return false;
201 }
202 return this->UploadRangeInternal(array, offset, numElements * sizeof(T), objectType);
203}
204
205template <class T>
206inline bool vtkOpenGLBufferObject::Download(T* array, size_t numElements)
207{
208 return this->DownloadRangeInternal(array, 0, numElements * sizeof(T));
209}
210
211template <class T>
212inline bool vtkOpenGLBufferObject::DownloadRange(T* array, ptrdiff_t offset, size_t numElements)
213{
214 return this->DownloadRangeInternal(array, offset, numElements * sizeof(T));
215}
216
217VTK_ABI_NAMESPACE_END
218#endif
object to represent cell connectivity
a simple class to control print indentation
Definition vtkIndent.h:108
void SetType(ObjectType value)
Set the type of the buffer object.
bool IsReady() const
Determine if the buffer object is ready to be used.
bool Release()
Release the buffer.
ObjectType GetType() const
Get the type of the buffer object.
bool BindShaderStorage(int index)
Bind the buffer to a shader storage point.
~vtkOpenGLBufferObject() override
ObjectUsage GetUsage() const
Get the usage of the buffer object.
bool DownloadRange(T *array, ptrdiff_t offset, size_t numElements)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool Bind()
Bind the buffer object ready for rendering.
bool UploadInternal(const void *buffer, size_t size, ObjectType objectType)
bool Upload(const T &array, ObjectType type)
Upload data to the buffer object.
bool GenerateBuffer(ObjectType type)
Generate the opengl buffer for this Handle.
bool Allocate(size_t size, ObjectType type, ObjectUsage usage)
Allocates a buffer of type with size bytes.
int GetHandle() const
Get the handle of the buffer object.
static vtkOpenGLBufferObject * New()
bool UploadRange(const T &array, ptrdiff_t offset, ObjectType type)
bool Download(T *array, size_t numElements)
Download data from the buffer object.
std::string GetError() const
Return a string describing errors.
size_t GetSize()
Get size of the buffer in bytes.
void FlagBufferAsDirty()
Indicate that the buffer object needs to be re-uploaded.
void SetUsage(ObjectUsage value)
Set the usage of the buffer object.
bool UploadRangeInternal(const void *buffer, ptrdiff_t offset, ptrdiff_t size, ObjectType objectType)
represent and manipulate 3D points
Definition vtkPoints.h:140
#define vtkDataArray