VTK  9.4.20241219
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
24class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLBufferObject : public vtkObject
25{
26public:
29 void PrintSelf(ostream& os, vtkIndent indent) override;
30
32 {
35 TextureBuffer
36 };
38 {
47 DynamicCopy
48 };
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(&array[0], array.size() * sizeof(typename T::value_type), objectType);
165}
166
167template <class T>
169 const T* array, size_t numElements, vtkOpenGLBufferObject::ObjectType objectType)
170{
171 if (!array)
172 {
173 this->Error = "Refusing to upload empty array.";
174 return false;
175 }
176 return this->UploadInternal(array, numElements * sizeof(T), objectType);
177}
178
179template <class T>
181 const T& array, ptrdiff_t offset, vtkOpenGLBufferObject::ObjectType objectType)
182{
183 if (array.empty())
184 {
185 this->Error = "Refusing to upload empty array.";
186 return false;
187 }
188
189 return this->UploadRangeInternal(
190 &array[0], offset, array.size() * sizeof(typename T::value_type), objectType);
191}
192template <class T>
193inline bool vtkOpenGLBufferObject::UploadRange(const T* array, ptrdiff_t offset, size_t numElements,
195{
196 if (!array)
197 {
198 this->Error = "Refusing to upload empty array.";
199 return false;
200 }
201 return this->UploadRangeInternal(array, offset, numElements * sizeof(T), objectType);
202}
203
204template <class T>
205inline bool vtkOpenGLBufferObject::Download(T* array, size_t numElements)
206{
207 return this->DownloadRangeInternal(array, 0, numElements * sizeof(T));
208}
209
210template <class T>
211inline bool vtkOpenGLBufferObject::DownloadRange(T* array, ptrdiff_t offset, size_t numElements)
212{
213 return this->DownloadRangeInternal(array, offset, numElements * sizeof(T));
214}
215
216VTK_ABI_NAMESPACE_END
217#endif
object to represent cell connectivity
abstract superclass for arrays of numeric data
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
OpenGL buffer object.
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:139