VTK  9.7.1
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 
12 VTK_ABI_NAMESPACE_BEGIN
13 class vtkCellArray;
14 class vtkDataArray;
15 class vtkPoints;
16 
24 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLBufferObject : public vtkObject
25 {
26 public:
27  static vtkOpenGLBufferObject* New();
29  void PrintSelf(ostream& os, vtkIndent indent) override;
30 
32  {
35  TextureBuffer
36  };
38  {
47  DynamicCopy
48  };
50  ObjectType GetType() const;
51 
53  void SetType(ObjectType value);
54 
56  ObjectUsage GetUsage() const;
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 
71  bool GenerateBuffer(ObjectType type);
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.
128  void ReleaseGraphicsResources();
129 
133  std::string GetError() const { return Error; }
134 
135 protected:
137  ~vtkOpenGLBufferObject() override;
138  bool Dirty;
140 
141  bool UploadInternal(const void* buffer, size_t size, ObjectType objectType);
142  bool UploadRangeInternal(
143  const void* buffer, ptrdiff_t offset, ptrdiff_t size, ObjectType objectType);
144 
145 private:
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 
154 template <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 
168 template <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 
180 template <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 }
193 template <class T>
194 inline 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 
205 template <class T>
206 inline bool vtkOpenGLBufferObject::Download(T* array, size_t numElements)
207 {
208  return this->DownloadRangeInternal(array, 0, numElements * sizeof(T));
209 }
210 
211 template <class T>
212 inline bool vtkOpenGLBufferObject::DownloadRange(T* array, ptrdiff_t offset, size_t numElements)
213 {
214  return this->DownloadRangeInternal(array, offset, numElements * sizeof(T));
215 }
216 
217 VTK_ABI_NAMESPACE_END
218 #endif
bool DownloadRange(T *array, ptrdiff_t offset, size_t numElements)
bool UploadRangeInternal(const void *buffer, ptrdiff_t offset, ptrdiff_t size, ObjectType objectType)
abstract base class for most VTK objects
Definition: vtkObject.h:52
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
std::string GetError() const
Return a string describing errors.
bool Download(T *array, size_t numElements)
Download data from the buffer object.
a simple class to control print indentation
Definition: vtkIndent.h:28
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
void FlagBufferAsDirty()
Indicate that the buffer object needs to be re-uploaded.
object to represent cell connectivity
Definition: vtkCellArray.h:177
bool IsReady() const
Determine if the buffer object is ready to be used.
OpenGL buffer object.
bool Upload(const T &array, ObjectType type)
Upload data to the buffer object.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool UploadRange(const T &array, ptrdiff_t offset, ObjectType type)
bool UploadInternal(const void *buffer, size_t size, ObjectType objectType)
represent and manipulate 3D points
Definition: vtkPoints.h:30